提交请求后不能执行的操作

本文关键字:操作 执行 不能 请求 提交 | 更新日期: 2023-09-27 17:50:54

我成功地在手持设备(compact Framework/Windows CE)和现代(不是Windows 8中的"现代",而是21世纪的)服务器应用程序(Web API)之间传递数据。然而,在(成功)传递数据后,客户端失败并返回err消息,"在提交请求后,此操作不能执行"

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

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()");

…是不必要的,但显然我甚至没有到达那里,因为我从来没有看到过这条信息;我只是看到前面提到的错误,然后"错误在Bla.exe中发生了意想不到的错误选择相当,然后重新启动此程序,或选择详细信息"

详细信息是:"Error Bla.exe ObjectDisposedException at system . threading . waithhandle . "CheckResultInternal(Boolean r) at…"

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

那么是什么出错了?

更新我不明白大拇指对这个问题的反应。

关于这一切的另一个奇怪的事情是,即使我"遇到了一个严重的错误",它似乎并不像其他时候手持应用程序崩溃那样严重,在这种情况下,我必须经常热启动设备和/或在旧版本上复制新版本的。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…然后…??

更新4

好的,arrData。作品长度。

提交请求后不能执行的操作

您应该在调试器下运行代码,并观察它在哪一行出错。

GetRequestStream不应被多次调用。你应该考虑这样修改你的代码:

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