使用POST将简单字符串发送到WCF服务

时间:2015-01-07 07:51:38

标签: c# android json wcf http-post

我在android中使用POST方法向WCF服务发送字符串。像这样,

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setHeader("Content-Type", "application/xml");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);

在服务器端我正在这样做以获得该字符串。

[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "{userId}/postlogs",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
void PostLogs(string userId, RequestLogsData message);

我知道我做错了什么,但我不知道它是什么。 Android没有给出任何错误,但这不是将数据发送到服务。

告诉我用json或xml发布这个单字符串的方法。

这是我的请求类。问题在于我认为。

[DataContract (Namespace="asd")]
public class RequestLogsData
{
    [DataMember]
    public string message { get; set; }
}

通过chrome extension rest console检查我的服务,将其作为Raw body

发送
<RequestLogsData xmlns="asd">
<message>
Messag Post
</message>
</RequestLogsData>

并将内容类型设置为:application/xml

这就按预期调用了服务,现在只是引导我如何通过Android应用程序调用它。

0 个答案:

没有答案
相关问题