简体   繁体   中英

c# wup Post Request with urlencoded Content

i try to execute a Post Request with Postdata, wich are partial 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 _ instaed of & additionally and nevertheless i'm using UTF8 encoding Charakters like äüö are broken.

I Tryed 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM