我有一个Android应用程序。我想向WCF Web服务发送请求。
服务是:
[OperationContract]
Message submitData(Stream streamdata);
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "submitData")]
public Message submitData(Stream streamdata)
{
StreamReader reader = new StreamReader(streamdata);
string jsonString = reader.ReadToEnd();
reader.Close();
reader.Dispose();
return WebOperationContext.Current.CreateTextResponse(JsonSyncManager.submitVotes(jsonString), "application/json; charset=utf-8", Encoding.UTF8);
}
我在Fiddler上尝试了这项服务,但它运行正常。
但是,从我的应用程序中,我尝试以下
JSONObject j = new JSONObject();
j.put("voters", "test");
String url = "http://127.0.0.1:8733/Service/submitData";
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
StringEntity s = new StringEntity(j.toString(), "UTF-8");
s.setContentType("application/json;charset=UTF-8");
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "application/json; charset=utf-8");
request.setEntity(s);
HttpResponse response = httpclient.execute(request);
但我总是收到错误请求错误。它没有到达服务(不会进入断点)。
知道问题可能是什么?
由于
答案 0 :(得分:0)
相同问题:400错误请求。 尝试更改内容类型:
request.setHeader("Content-Type", "application/json");
它解决了我的问题。