簡體   English   中英

在 Android 中通過 SSL 使用 WCF 服務

[英]Consume WCF Service over SSL in Android

我一直在使用WCF (.svc) 服務,在一個運行良好的 Android 應用程序中,請求格式為JSON ,響應格式為XML 幾天前,我在 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" %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM