java - 在 Android 中通过 SSL 使用 WCF 服务

标签 java c# android wcf ssl

我一直在使用 WCF (.svc) 服务一段时间,请求格式为 JSON 响应格式为 XML一个运行良好的 Android 应用程序。几天前,我在 DigiCert 的 WCF 服务上实现了一个用于 SSL 目的的证书(使用我的通配符功能)。该服务可从浏览器访问并且没有显示错误。下面是 WebConfig

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="IntermediateWebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
      <customErrors mode="Off"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
          <behavior name="IntermediateWebService.WebBehavior">
              <serviceMetadata httpsGetEnabled="true" />

              <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        <behavior>

          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="WebBehavior">
            </behavior>
        </endpointBehaviors>
    </behaviors>
      <bindings>
          <basicHttpBinding>
              <binding name="secureHttpBinding" maxBufferPoolSize="524288" maxReceivedMessageSize="999999999">
                  <security mode="Transport">
                      <transport clientCredentialType="None"/>
                  </security>
              </binding>
          </basicHttpBinding>
          <webHttpBinding>
              <binding name="webHttpBindingConfig">
                  <readerQuotas maxStringContentLength="2048000" />
              </binding>
          </webHttpBinding>
      </bindings>
      <services>

          <service behaviorConfiguration="IntermediateWebService.WebBehavior" name="IntermediateWebService.Service1">
              <host>
                  <baseAddresses>
                      <add baseAddress="https://myurl:4445/"/>
                  </baseAddresses>
              </host>
              <endpoint address="" binding="basicHttpBinding" contract="IntermediateWebService.IService1" behaviorConfiguration="WebBehavior" bindingConfiguration="secureHttpBinding" />
          </service>
          <!--<service name="IntermediateWebService.Service1">
               <endpoint address=""
      binding="basicHttpBinding"
      bindingConfiguration="secureHttpBinding"
      contract="IntermediateWebService.IService1"/>



          </service>-->
      </services>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
 <system.webServer>
     <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

所以现在在使用相同的 Android 代码时,响应始终是

The server cannot service the request because the media type is unsupported.

我试过使用 SSL Factory 和不使用它。

HttpClient client = getHttpsClient(new DefaultHttpClient()); //new DefaultHttpClient();

            HttpPost get = null;
            commandType = params[0].toString();
            if ("Login".equals(params[0])){

                JSONStringer img = new JSONStringer()
                        .object()
                        .key("value")
                            .object()
                                .key("username").value(params[1].toString())
                                .key("pwd").value(params[2].toString())
                                .key("channelID").value(params[3].toString())
                            .endObject()
                        .endObject();
                        StringEntity se = new StringEntity(img.toString());

                get = new HttpPost("https://" + serverIP + ":" + serverPort + "/Service1.svc/auth");
                //get.setHeader("User-Agent", "com.app.new");
                get.setHeader("Accept", "application/json");
                get.setHeader("Content-Type", "application/json");
                get.setEntity(se);

最佳答案

当我探索返回状态代码 415 的错误时,WCF 服务以某种方式不接受该请求作为有效 JSON,因此通过在 svc 中添加以下属性标记起到了作用

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

这是完整的标记

<%@ ServiceHost Language="C#" Debug="true" Service="IntermediateWebService.Service1" CodeBehind="Service1.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

关于java - 在 Android 中通过 SSL 使用 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327380/

相关文章:

android - java.lang.OutOfMemoryError : bitmap size exceeds VM budget in ListView and lazy loading images 错误

java - JNI/ Kotlin : Is it possible to pass delegate to JNI?

java - 新标签页无法使用 selenium 在 Firefox 中打开新网址

C# string[] 到 int[]

c# - 使用正则表达式处理 bool 短语

c# - 我如何知道我从流中获得的图像格式?

java - 如何向 Lucene/Solr 相似性类添加新方法?

java - 为什么我的快速排序这么慢?

android - 如何获取 Android 上具有日历的帐户列表?

android - AlarmManager 未决 Intent 不适用于广播接收器