WebClient中的Post方法 [英] Post Method in Webclient

查看:372
本文介绍了WebClient中的Post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

web service post方法对我来说是新的。可以帮我处理错误。

web service post method is new for me. can u pls help me out what am i doing wrong.

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附加它的工作正常

by the way, If i access the url with the parameters and value appended its working fine

像这样(http:// localhost:60696 / service / getlogin / username,123,password,456)。

推荐答案

postdata似乎错误编码,应该是这样:

The postdata seems wrongly encoded, should be something like this:

postData.AppendFormat("{0}={1}", "username", HttpUtility.UrlEncode(textBox1.Text));
postData.AppendFormat("&{0}={1}", "password", HttpUtility.UrlEncode(textBox2.Text));

这篇关于WebClient中的Post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆