【问题标题】:Bizzare HttpWebResponse Error: ServerProtocolViolationBizzare HttpWebResponse 错误:ServerProtocolViolation
【发布时间】:2011-06-18 05:10:26
【问题描述】:

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

背景: 我有一个 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();
}

【问题讨论】:

  • 这意味着服务器表现不佳 :) 你能使用像 Fiddler 或 Ethereal 这样的 HTTP 嗅探器来查看原始响应是什么吗?
  • 它工作了 22 次,然后停止。如果我杀死我的应用程序并重新开始......对于另外 22 个请求来说一切都很好。你还认为它在服务器上?

标签: c# mono httpwebrequest xamarin.ios httpwebresponse


【解决方案1】:
  • 客户端之间是否有代理 和服务器?
  • 22 次请求后总是失败吗? 异常表示某些 UInt32 无法解析。
  • 您是否在服务器端遇到异常?

【讨论】:

  • 无代理,约 22 个请求,无服务器异常。
  • 可以显示客户端代码吗?您可能会错误地在客户端重复使用某些东西。
  • 考虑到异常和 304 状态码,最好的猜测是在这种情况下删除任何描述性文本。
  • 删除 Response.Write。见stackoverflow.com/questions/602104/…
  • 它与内容类型有关,我将其设置为 null,一切都很好。感谢您为我指明正确的方向。
【解决方案2】:

如果其他人正在互联网上寻找有关此问题的答案,我在编写的一些功能测试中遇到了非常相似的问题。他们使用 http PUT 将内容上传到网络服务器,然后直接使用 http GET。

GET 可靠地失败,在 Uint32 解析中出现相同的错误。 windows的构建很好。绝望中我使用 Thread.Sleep(20) 在 http 请求之间插入一些延迟,这已经清除了错误。不知道为什么,但它现在可以工作了,这对于我的测试来说已经足够了,如果不是用于生产的话。

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 2017-09-21
    • 2019-11-21
    • 2018-08-29
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2015-04-19
    • 2011-03-25
    相关资源
    最近更新 更多