Http post returns error 400 bad request, this is my code, can you see where I'm going wrong? I'm guessing it's something to do with the url?
System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
data["var1"] = source1;
data["var2"] = source2;
data["var3"] = source3;
// HTTP post and redirect the values
string str2 = string.Empty;
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (string str3 in data.AllKeys)
{
builder.Append(str2);
builder.Append(HttpUtility.UrlEncode(str3));
builder.Append("=");
builder.Append(HttpUtility.UrlEncode(data[str3]));
str2 = "&";
}
//Make a request
string path = Request.Url.AbsoluteUri;
string url = path + Target + builder;
url = "http://localhost:1541/test/var1=SELEZIONA&var2=SELEZIONA&var3=SELEZIONA";
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//Put the post data into the request
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(builder.ToString());
request.ContentLength = byteArray.Length;
Stream reqStream = request.GetRequestStream();
reqStream.Write(byteArray, 0, byteArray.Length);
reqStream.Close();
//Get response
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
I've put the actual url content in the url variable to show what it is currently trying to send.
You are lacking a questionmark in the URL to separate the querystring. I guess that could cause this kind of error. Try this instead:
url = "http://localhost:1541/test/?var1=SELEZIONA&var2=SELEZIONA&var3=SELEZIONA";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.