【问题标题】:Bad request when calling a WCF webservice调用 WCF Web 服务时出现错误请求
【发布时间】:2013-05-29 17:21:06
【问题描述】:

我有一个安卓应用程序。我想向 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);

但我总是收到错误的请求错误。它没有到达服务(不会进入断点)。

知道可能是什么问题吗?

谢谢

【问题讨论】:

  • 尝试在服务器端启用跟踪 - 跟踪应该告诉您为什么请求被认为是错误的。

标签: android wcf post


【解决方案1】:

同样的问题:400 错误请求。 尝试更改 Content-Type:

request.setHeader("Content-Type", "application/json");

它为我解决了问题。

【讨论】:

    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多