Windows phone 7 Webclient中的Post方法
web服务发布方法对我来说是新的。你能帮我找出我做错了什么吗Windows phone 7 Webclient中的Post方法,windows-phone-7,c#-4.0,silverlight-4.0,webclient,webclient.uploaddata,Windows Phone 7,C# 4.0,Silverlight 4.0,Webclient,Webclient.uploaddata,web服务发布方法对我来说是新的。你能帮我找出我做错了什么吗 WebClient webClient = new WebClient(); webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; webClient.Encoding = Encoding.UTF8; webClient.UploadString
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
webClient.Encoding = Encoding.UTF8;
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler((sender, e) =>
{
if (e.Error != null)
{
return;
}
string result = e.Result;
});
string uri = "http://localhost:60696/service/getlogin/";
StringBuilder postData = new StringBuilder();
postData.AppendFormat("/{0}/{1}", "username", HttpUtility.UrlEncode(textBox1.Text));
postData.AppendFormat("/{0}/{1}", "password", HttpUtility.UrlEncode(textBox2.Text));
webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
webClient.UploadStringAsync(new Uri(uri, UriKind.RelativeOrAbsolute),"POST", postData.ToString());
顺便说一句,如果我访问带有参数和附加值的url,它的工作状态会很好
像这样
(“http://localhost:60696/service/getlogin/username,123,密码,456”)。
postdata似乎编码错误,应该是这样的:
postData.AppendFormat("{0}={1}", "username", HttpUtility.UrlEncode(textBox1.Text));
postData.AppendFormat("&{0}={1}", "password", HttpUtility.UrlEncode(textBox2.Text));
如果您需要我的建议,只需使用RestSharp而不是手工提出请求,这将为您节省大量时间和头痛!!!雷斯特夏普?。。。我以前没用过。你能给我发一份关于它是什么以及如何使用它的参考资料给其他可能有同样问题但解决方案不起作用的人吗:检查服务器上的重定向规则。我的请求转到,服务器向我发送了一个“301永久移动”=>然后会再次发送WebRequest,但它似乎是作为GET请求发送的,因此您的所有帖子数据都会丢失!!