-2

我成功地在手持设备(Compacted Framework/Windows CE)和现代(不是 Windows 8 中的“现代”,而是 21 世纪)服务器应用程序(Web API)之间传递数据。但是,在(成功)传递数据后,客户端失败并显示 err msg,“ This operation cannot be executed after the request has been submit

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

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 Select Quite 然后重新启动这个程序,或者选择Details了解更多信息。

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

然后,“ 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 ......然后......???

更新 4

好的, arrData.Length 有效。

4

1 回答 1

1

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

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

byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream()) 
{
     oS.Write(arrData, 0, arrData.Length);
}
于 2014-03-18T22:36:31.023 回答