将字符串从 android 传递到 wcf restful 服务变得空
Posted
技术标签:
【中文标题】将字符串从 android 传递到 wcf restful 服务变得空【英文标题】:Passing a string from android to wcf restful service is getting null 【发布时间】:2017-06-23 11:38:45 【问题描述】:我有一个 wcf 网络服务,当我从 android 以 POST 方式访问网络服务时,服务中的值为 null。
cs文件如下:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Login", BodyStyle = WebMessageBodyStyle.Bare)] string Login(Student Student); public class Student [DataMember(Name = "image")] public string image get; set;
下面给出配置文件
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647" />
</webServices>
</scripting>
</system.web.extensions>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true"> </defaultProxy>
</system.net>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="ESIService.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="ESIService.IService1" behaviorConfiguration="web"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="500" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="16" maxConcurrentInstances="41" maxConcurrentSessions="100"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false"/>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true" />
<defaultDocument>
<files>
<add value="Service1.svc" />
</files>
</defaultDocument>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
</configuration>`
下面给出了 svc 文件,运行时得到的 id 值为 null。
public string Login(Student Student) String id = Student.image;
Android代码如下:
JSONStringer item = new JSONStringer()
.object()
.key("Student")
.object()
.key("image").value("abcd")
.endObject()
.endObject();
正在点击的 URL 是 .../Login
【问题讨论】:
【参考方案1】:我不熟悉 android 开发,但由于您有 BodyStyle = WebMessageBodyStyle.Bare,该服务在请求正文中需要以下 json 字符串:
"图像":"abcd"
也许这会起作用:
JSONStringer item = new JSONStringer()
.object()
.key("image").value("abcd")
.endObject();
【讨论】:
以上是关于将字符串从 android 传递到 wcf restful 服务变得空的主要内容,如果未能解决你的问题,请参考以下文章
将数据从 jquery ajax 请求传递到 wcf 服务失败反序列化?