我正在尝试将 json 字符串从我的 android 客户端(Android 4.03)发送到我的 .net Rest 服务(WCF)。我得到了“错误请求”作为回应。我有这个服务的 .NET 测试器,它工作正常。
服务器端代码:
[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)