2012-05-22 63 views
0

我想从我的Android客户端(Android 4.03) 发送一个json字符串到我的.net休息服务(WCF)。作为回应,我收到了“不良要求”。我有这个服务的.NET测试,它工作正常。错误的请求:从Android客户端发送字符串到.NET REST服务

服务器端代码:

[WebInvoke(UriTemplate = "submit", Method = "POST", RequestFormat = WebMessageFormat.Json)] 
public bool SubmitPackage(string instance) 
{  

log.WriteWarning("Submit"); 
return true; 
} 

我WCF配置

<standardEndpoints> 
    <webHttpEndpoint> 
     <standardEndpoint name="" maxReceivedMessageSize="327680" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" transferMode="Buffered" /> 
    </webHttpEndpoint> 
</standardEndpoints> 

我的Android客户端代码

HttpPost request2 = new HttpPost(URL_Submit); 
request2.setHeader("content-type", "application/json"); 

JSONStringer json1 = new JSONStringer().object().key("instance") 
       .value("hello world").endObject(); 


StringEntity entity1 = new StringEntity(json1.toString()); 
entity1.setContentType("application/json;charset=UTF-8"); 
entity1.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, 
       "application/json;charset=UTF-8")); 

request2.setEntity(entity1); 
// Send request to WCF service 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
String response1 = httpClient.execute(request2) 

回答

0

是您的WCF服务托管在IIS?打开失败的请求日志并查看结果。

也使用小提琴来查看发送的流量。您可以配置Fiddler以在服务器上运行。它通常运行在客户端上,但无法在Android上运行。

配置小提琴手在服务器上运行,

http://www.fiddler2.com/fiddler/help/reverseproxy.asp

而且我经常把这个在我的web.config,

<system.diagnostics> 
    <sources> 
     <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"> 
     <listeners> 
      <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\logs\Traces.svclog" /> 
     </listeners> 
     </source> 
    </sources> 
    </system.diagnostics> 

它记录有关请求的错误。这可以打开,请求解析。

相关问题