使用HttpWebRequest发布表单数据

我想将一些表单数据发布到不在我自己的Web应用程序中的指定URL。 它具有相同的域,如“domain.client.nl”。 Web应用程序有一个url“web.domain.client.nl”我想要发布到的URL是“idp.domain.client.nl”。 但是我的代码什么也没有做…..有人知道我在做什么错了吗?

沃特

StringBuilder postData = new StringBuilder(); postData.Append(HttpUtility.UrlEncode(String.Format("username={0}&", uname))); postData.Append(HttpUtility.UrlEncode(String.Format("password={0}&", pword))); postData.Append(HttpUtility.UrlEncode(String.Format("url_success={0}&", urlSuccess))); postData.Append(HttpUtility.UrlEncode(String.Format("url_failed={0}", urlFailed))); ASCIIEncoding ascii = new ASCIIEncoding(); byte[] postBytes = ascii.GetBytes(postData.ToString()); // set up request object HttpWebRequest request; try { request = (HttpWebRequest)HttpWebRequest.Create(WebSiteConstants.UrlIdp); } catch (UriFormatException) { request = null; } if (request == null) throw new ApplicationException("Invalid URL: " + WebSiteConstants.UrlIdp); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postBytes.Length; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; // add post data to request Stream postStream = request.GetRequestStream(); postStream.Write(postBytes, 0, postBytes.Length); postStream.Flush(); postStream.Close(); 

字段名称和值都应该被url编码。 发布数据和查询string的格式是一样的

.net的做法是这样的

 NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty); outgoingQueryString.Add("field1","value1"); outgoingQueryString.Add("field2", "value2"); string postdata = outgoingQueryString.ToString(); 

这将负责编码字段和值的名称

您正在编码forms不正确。 你只应该编码的值:

 StringBuilder postData = new StringBuilder(); postData.Append("username=" + HttpUtility.UrlEncode(uname) + "&"); postData.Append("password=" + HttpUtility.UrlEncode(pword) + "&"); postData.Append("url_success=" + HttpUtility.UrlEncode(urlSuccess) + "&"); postData.Append("url_failed=" + HttpUtility.UrlEncode(urlFailed)); 

编辑

我错了。 根据RFC1866第8.2.1节的规定 ,名字和数值都应该被编码。

但是对于给定的例子,名称没有任何需要编码的字符,所以在这种情况下,我的代码示例是正确的;)

问题中的代码仍然不正确,因为它会对等号进行编码,这就是为什么Web服务器无法解码的原因。

更正确的方法是:

 StringBuilder postData = new StringBuilder(); postData.AppendUrlEncoded("username", uname); postData.AppendUrlEncoded("password", pword); postData.AppendUrlEncoded("url_success", urlSuccess); postData.AppendUrlEncoded("url_failed", urlFailed); //in an extension class public static void AppendUrlEncoded(this StringBuilder sb, string name, string value) { if (sb.Length != 0) sb.Append("&"); sb.Append(HttpUtility.UrlEncode(name)); sb.Append("="); sb.Append(HttpUtility.UrlEncode(value)); } 

尝试这个:

 var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx"); var postData = "thing1=hello"; postData += "&thing2=world"; var data = Encoding.ASCII.GetBytes(postData); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();