1

网络服务发布方法对我来说是新的。你能帮我弄清楚我做错了什么吗?

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,password,456").

4

1 回答 1

2

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

postData.AppendFormat("{0}={1}", "username", HttpUtility.UrlEncode(textBox1.Text));
postData.AppendFormat("&{0}={1}", "password", HttpUtility.UrlEncode(textBox2.Text));
于 2012-09-21T10:58:43.280 回答