【问题标题】:Which operation cannot be performed after the request has been submitted?请求提交后哪些操作不能执行?
【发布时间】:2023-03-03 00:06:01
【问题描述】:

我成功地在手持设备(Compacted Framework/Windows CE)和现代(不是 Windows 8 中的“现代”,而是 21 世纪)服务器应用程序 (Web API) 之间传递数据。但是,在(成功)传递数据后,客户端失败并显示 err msg,“此操作在请求提交后无法执行

它在抱怨哪个操作?这是我的客户端代码:

public static HttpWebResponse SendXMLFile(string xmlFilepath, string uri) 
{
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
    }

    string strData = @sb.ToString(); 
    strData = strData.Replace("\"", "'"); 
    string body = String.Format("\"{0}\"", strData);
    HttpWebRequest request = CreateRequestNoCredentials(uri, HttpMethods.POST, body, "application/json"); 

    MessageBox.Show("Made it past the call to CreateRequestNoCredentials()");
    try
    {
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            MessageBox.Show(string.Format("ReadResponse val = {0}", RESTfulMethods.ReadResponse(response)));
            return response;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("Msg = {0}; StackTrace = {1}", ex.Message, ex.StackTrace));
        request.Abort();
        return null;
    }
}

public static HttpWebRequest CreateRequestNoCredentials(string uri, HttpMethods method, string data, string contentType)
{
    //test:
    MessageBox.Show(string.Format("In CreateRequestNoCredentials(); data passed in = {0}", data));

    WebRequest request = HttpWebRequest.Create(uri);
    request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();
    request.ContentType = contentType;
    ((HttpWebRequest)request).Accept = contentType;
    ((HttpWebRequest)request).KeepAlive = false;
    ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;

    if (method != HttpMethods.GET && method != HttpMethods.DELETE)
    {
        Encoding encoding = Encoding.UTF8;
        request.ContentLength = encoding.GetByteCount(data);
        request.GetRequestStream().Write(
          encoding.GetBytes(data), 0, (int)request.ContentLength);
        request.GetRequestStream().Close();
    }
    else
    {
        // If we're doing a GET or DELETE don't bother with this 
        request.ContentLength = 0;
    }
    // Finally, return the newly created request to the caller. 
    return request as HttpWebRequest;
}

可能是以下代码:

MessageBox.Show("Made it past the call to CreateRequestNoCredentials()");

... 是不必要的,但我显然还没有到达那里,因为我从未看到该消息;刚刚看到前面提到的错误,然后“Error An unexpected error has occurred in Bla.exe 选择Quite然后重启这个程序,或者选择Details了解更多信息。

详细信息是:“Error Bla.exe ObjectDisposedException at System.Threading.WaitHandle.CheckResultInternal(Boolean r) at...(etc.)

然后,“Application Bla.exe 遇到严重错误,必须关闭。

那么什么是绊脚石?

更新

我不明白leadthumb 对这个问题的回答。

另外一件奇怪的事情是,即使我“遇到了一个严重的错误”,它似乎并不像其他时候手持应用程序崩溃那么严重,在这种情况下,我必须经常热启动设备和/或在将新版本的 .exe 复制到旧版本之前将设备重新安装在其底座中。但这个“严重”错误并没有发生这种情况。

我简化了代码,使其不再返回 HttpWebResponse,但我仍然得到相同的结果。然而,我想要发生的事情很好:数据被发送到服务器应用程序,它成功地从传递的数据重新创建一个 XML 文件,然后通过根据传递的值插入一条新记录来更新数据库。

这是新的、精简的代码:

public static void SendXMLFile(string xmlFilepath, string uri, int timeout) // timeout should be 500
{
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
    }

    string strData = @sb.ToString();
    strData = strData.Replace("\"", "'");
    string body = String.Format("\"{0}\"", strData);
    CreateRequestNoCredentials(uri, HttpMethods.POST, body, "application/json"); 
}

更新 2

如果我以这种方式捕获并忽略异常:

catch (Exception ex)
{
    if (ex.Message.Contains("This operation cannot be performed after the request has been submitted"))
    {
        // just make like Johnny Bench
    }
}

...我可以毫无问题地运行该方法;但是,当我关闭应用程序时,我收到“Bla.exe 发生意外错误。选择退出,然后重新启动此程序,或选择详细信息以获取更多信息。”

更新 3

建议的替换代码给了我,“无法将类型 'byte[]' 隐式转换为 'long'

改成这样:

request.ContentLength = arrData;

...给我,“无法将类型 'byte[]' 转换为 'long'

所以它不能被 im- 或显式转换为 long...then...???

更新 4

好的,arrData.Length 有效。

【问题讨论】:

  • 一定有人发现我曾经有一件“No Disco”的T恤;或者可能是“禁止高尔夫”T 恤……

标签: c# asp.net-web-api httpwebrequest httpwebresponse compact-framework2.0


【解决方案1】:

您应该在调试器下运行您的代码并观察它在哪一行失败。

GetRequestStream 不应被多次调用。您应该考虑将代码更改为:

byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream()) 
{
     oS.Write(arrData, 0, arrData.Length);
}

【讨论】:

  • 我无法单步执行此代码 - 它在手持设备上运行,没有可用的模拟器。
  • 这段代码并不神奇;如果需要,您可以在 Windows 8.1 上的 .NET 4.5 中运行它。
  • 请参阅更新 3...没关系,arrData.Length 有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 2015-12-23
相关资源
最近更新 更多