Java 在Android中通过SSL使用WCF服务
我已经使用了一段时间的Java 在Android中通过SSL使用WCF服务,java,c#,android,wcf,ssl,Java,C#,Android,Wcf,Ssl,我已经使用了一段时间的WCF(.svc)服务,它的请求格式是JSON,响应格式是XML,在Android应用程序中运行良好。几天前,我在DigiCert的WCF服务上实现了一个用于SSL目的的证书(使用我的通配符功能)。可以从浏览器访问该服务,并且不会显示任何错误。下面是网络配置 <?xml version="1.0"?> <configuration> <configSections> <sectionGroup name="applica
WCF
(.svc)服务,它的请求格式是JSON
,响应格式是XML
,在Android应用程序中运行良好。几天前,我在DigiCert的WCF
服务上实现了一个用于SSL
目的的证书(使用我的通配符功能)。可以从浏览器访问该服务,并且不会显示任何错误。下面是网络配置
<?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>
我尝试过使用SSL工厂,也尝试过不使用它
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" %>
在或处尝试我的一些答案,看看它们是否有帮助。目标android平台是否支持tls版本?请尝试此答案。尝试使用此标题来请求“Content-Type”、“text/xml;charset=utf-8”您是否尝试从浏览器以外的其他地方发出请求,并看到它工作正常?例如,如果您正在使用谷歌浏览器,请尝试名为“Postman RestClient”的插件og“Advanced RestClient”。查看它是否与Java代码中设置的标题和POST
body相同。我想说的是,弄清楚这是否是Android/Java的问题。如果它仍在工作,试着用OkHttp
替换你的HTTP客户端,看看这是否有什么不同。您也可以向我们公开您的服务,如果您愿意,我们可以“试驾”它;-)
<%@ ServiceHost Language="C#" Debug="true" Service="IntermediateWebService.Service1" CodeBehind="Service1.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>