gpt4 book ai didi

c# - 奇怪的 HttpWebResponse 错误 : ServerProtocolViolation

转载 作者:太空狗 更新时间:2023-10-29 23:34:35 33 4
gpt4 key购买 nike

我已经尝试了所有方法,但我无法弄清楚为什么会出现此错误。

背景:我有一个 iPad 应用程序,用 MonoTouch 编写,我有一个在后台运行的线程,每 15 秒我与服务器同步一次数据。这适用于线程的前几次迭代,但最终我得到以下堆栈跟踪。

An exception occured: System.Net.WebException: Error getting response stream (ReadDone4): ServerProtocolViolation ---> System.FormatException: Input string was not in the correct format
at System.UInt32.Parse (System.String s) [0x00010] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/UInt32.cs:405
at System.Net.WebConnection.GetResponse (System.Byte[] buffer, Int32 max) [0x000ba] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:565
at System.Net.WebConnection.ReadDone (IAsyncResult result) [0x00095] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:446
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:819
at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:827
at SyncService.REST.RestClient.Execute[IEnumerable`1] (SyncService.REST.RestRequest request) [0x00079] in /Users/Chris/Compass/SyncService/REST/RestClient.cs:42

我正在与具有默认配置的 IIS 网络服务器 对话。这是我调用的方法:

public RestResponse<T> Execute<T>(RestRequest request){
var restResponse = new RestResponse<T>();
var serializer = new JavaScriptSerializer();
var urlPath = _baseUrl + "/" + request.Resource;
var httpRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlPath));

httpRequest.Headers = request.Headers;
Authenticator.Authenticate(httpRequest);

httpRequest.Method = request.Method.ToString();
if (request.Method == Method.POST)
SetPostData(httpRequest, request);

HttpWebResponse httpResponse = null;
try{
httpResponse = (HttpWebResponse) httpRequest.GetResponse();
var reader = new StreamReader(httpResponse.GetResponseStream());
var responseString = reader.ReadToEnd();
reader.Close();
restResponse.StatusCode = httpResponse.StatusCode;
restResponse.Headers = httpResponse.Headers;
restResponse.Data = serializer.Deserialize<T>(responseString);
restResponse.ResponseStatus = ResponseStatus.Completed;
}
catch(WebException e){

restResponse.ResponseStatus = ResponseStatus.Error;
restResponse.ErrorMessage = e.Message;
restResponse.ErrorException = e;
var webResponse = (HttpWebResponse) e.Response;
if (webResponse != null){
restResponse.StatusCode = webResponse.StatusCode;
restResponse.Headers = webResponse.Headers;
}
if (restResponse.StatusCode != HttpStatusCode.NotModified)
Console.WriteLine("An exception occured: " + e + "\r\n");
}catch (Exception ex) {
restResponse.ResponseStatus = ResponseStatus.Error;
restResponse.ErrorMessage = ex.Message;
restResponse.ErrorException = ex;
}

if (httpResponse != null)
httpResponse.Close();

return restResponse;
}

请帮忙。我不知道该怎么办。 Google 什么也没显示。

在出现错误之前,我能够发出 22 次成功的请求。

编辑我已将其缩小为服务器问题。这是 asp.net MVC,只有当我向客户端发送 304 时才会发生异常。查看服务器代码:

private void ServeHttpStatusCode() {
Response.StatusCode = 304;
Response.Status = "304 Not Modified";
Response.StatusDescription = "The resource you are requesting has not been modified";
Response.ContentType = "application/json";
Response.Write("{\"Error\":\"The resource you are requesting has not been modified\"}");
Response.End();
Response.Close();
}

最佳答案

  • 客户端之间是否有代理和服务器?
  • 是否总是在 22 次请求后失败?该异常表明某些 UInt32 无法解析。
  • 您是否在服务器端收到异常?

关于c# - 奇怪的 HttpWebResponse 错误 : ServerProtocolViolation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4844984/

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