I tried to execute a Post Request with Postdata, which are partially urlencoded.
I have the following PostData to send:
p1=v1
p2=v2
p3=up1=uv1&up2=uv2
in order to achieve this, I format the value of p3
p3=up1%3duv1%26up2%3duv2
Then I write everything in one String value:
p1=v1&p2=v2&p3=up1%3duv1%26up2%3duv2
Now I have a Post Request:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["ContentLength"] = data.Length.ToString();
using (Stream stream = await request.GetRequestStreamAsync())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = await request.GetResponseAsync();
Stream streamResponse = response.GetResponseStream();
But the target URL now receives
p1=v1
p2=v2
P3=up1=uv1_up2=uv2
a _ instead of & additionally and nevertheless I'm using UTF8 encoding Characters like äüö are broken.
I tried several encodings (ASCII, iso-8859-1) but always the same.
The final question is, how to post a urlencoded String?
The final question is, how to post a urlencoded String ?
I cannot reproduce your issue. With the above client side code snippet, I can receive the correct post data which contains &
on the server side. I created a Web API which target .Net Framework 4.7 for testing and the Post()
method code as follows:
public async Task<object> Post()
{
var kvs = await Request.Content.ReadAsFormDataAsync();
return kvs.AllKeys.Select(k => new { key = k, value = kvs[k] });
}
And with your client side code snippet I can receive the correct return value as follow picture shows. My UWP app target build 16299.
So that the way you post the string contains &
should be correct. Your issue may be caused by your server side, if you still have issues please provide the server side details. Here is a similar thread you could also reference.