我成功地在手持设备(压缩框架/ Windows CE)和现代(不像Windows 8中的“现代”,但在21世纪)服务器应用程序(Web API)之间传递数据。但是,在(成功)传递数据之后,客户端失败并显示错误信息,“在提交请求后无法执行此操作”
它抱怨哪个操作?这是我的客户代码:
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中发生了意外错误选择Quite然后重新启动此程序,或选择Details以获取更多信息。”
详细信息:“错误Bla.exe ObjectDisposedException在System.Threading.WaitHandle.CheckResultInternal(布尔r)在......(等)”
然后,“ 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");
}
如果我以这种方式捕捉并忽略异常:
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中发生意外错误。选择退出然后重新启动此程序,或选择详细信息以获取更多信息。”
建议的替换代码告诉我,“无法将类型'byte []'隐式转换为'long'”
将其更改为:
request.ContentLength = arrData;
...给我,“无法将类型'byte []'转换为'long'”
所以不能将其显式转换为long ...然后...... ???
好的,arrData.Length可以工作。
答案 0 :(得分:1)
您应该在调试器下运行代码,并观察它失败的行。
GetRequestStream
不应多次调用。您应该考虑将代码更改为:
byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream())
{
oS.Write(arrData, 0, arrData.Length);
}