C# WebRequest一直在对我的数据进行编码

C# WebRequest一直在对我的数据进行编码,c#,.net,webrequest,C#,.net,Webrequest,我正在使用以下代码。我有一个问题,“parameters”是一个字符串,我在里面有空格字符,无论我是否使用HttpUtility.UrlDecode(parameters),在将其转换为字节之前,在Wireshark中,我看到postdata执行bla+bla+bla,而不是bla-bla-bla 这是什么原因造成的?编码 WebRequest request = WebRequest.Create(uri); // Set the Method property of the request

我正在使用以下代码。我有一个问题,“parameters”是一个字符串,我在里面有空格字符,无论我是否使用HttpUtility.UrlDecode(parameters),在将其转换为字节之前,在Wireshark中,我看到postdata执行bla+bla+bla,而不是bla-bla-bla

这是什么原因造成的?编码

WebRequest request = WebRequest.Create(uri);

// Set the Method property of the request to POST.
request.Method = "POST";

// Create POST data and convert it to a byte array.
string postData = parameters;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);


// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";

// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;

// Get the request stream.
Stream dataStream = request.GetRequestStream();

// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);

// Close the Stream object.
dataStream.Close();

// Get the response.
WebResponse response = request.GetResponse();

// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);

// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();

// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);

// Read the content.
string responseFromServer = reader.ReadToEnd();

// Display the content.
string res = responseFromServer;

// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

return res;

我刚刚运行了代码并使用Fiddler进行了调试,但它没有对空格进行编码,这可能是wireshark吗?不,因为我在这个应用程序中收到了一个请求,我正在将它转发到另一个服务器(也就是说,我的应用程序是代理)。传入的请求带有空格,我在这里写的帖子是doing+,如果你不想让你的数据被urlencoded,为什么要使用x-www-form-urlencoded?这告诉任何获取数据(或截取数据)的人数据是url编码的…尝试将ContentType设置为“text/plain”。更好的是,如果你的应用程序是一个代理,请重新使用原始的内容类型…嗨,传入的是x-www-form-Url编码的,这就是为什么我转发它的原因