gpt4 book ai didi

c# - System.Net.ProtocolViolationException 异常 C# Post 和 Getresponse

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:42 38 4
gpt4 key购买 nike

对于下面的代码,我得到以下错误,

System.Net.ProtocolViolationException: You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse.

我不确定为什么会抛出这个错误,任何意见或建议都会有所帮助

                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://");

// Set the ContentType property.
request.ContentType = "application/x-www-form-urlencoded";
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
request.KeepAlive = true;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
// Start the asynchronous operation.
request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);

// Keep the main thread from continuing while the asynchronous
// operation completes. A real world application
// could do something useful such as updating its user interface.
allDone.WaitOne();

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
Console.ReadLine();
// Close the stream object.
streamResponse.Close();
streamRead.Close();

// Release the HttpWebResponse.
response.Close();





private static void ReadCallback(IAsyncResult asynchronousResult)
{

HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

// End the operation.
Stream postStream = request.EndGetRequestStream(asynchronousResult);



// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
allDone.Set();
}

现在我修改了我的代码以使用 HttpClient 但不起作用,

    public static async void PostAsync(String postData)
{

try
{
// Create a New HttpClient object.
HttpClient client = new HttpClient();

HttpResponseMessage response = await client.PostAsync("http://", new StringContent(postData));
Console.WriteLine(response);
//response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method in following line
// string body = await client.GetStringAsync(uri);

Console.WriteLine(responseBody);

}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);

}
}

最佳答案

错误很可能是由于您混合了异步和同步操作。 HttpWebRequest.BeginGetRequestStream 的文档说:

Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the BeginGetRequestStream method, you must use the BeginGetResponse method to retrieve the response.

您的代码调用了 BeginGetRequestStream,但它调用了 GetResponse

我认为正在发生的是它调用 BeginGetRequestStream,它开始异步写入请求流,但在主线程上它同时调用 GetResponse。所以它试图在请求格式化之前发出请求。

研究链接的 MSDN 主题中的示例并相应地修改您的代码。

关于c# - System.Net.ProtocolViolationException 异常 C# Post 和 Getresponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192423/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com