0
在下面的代碼中,DoGet工作非常穩定。但DoPost隨機拋出一個不可撤銷的InvalidOperationException。我搞不清楚了。任何指針都會有巨大的幫助。c#smartdevice - HTTPWEBREQUEST GET與POST InvalidOperationException
/*
Environment
-------------
* NET CF 2.0
* WM 5.0(USA Mobile Pocket PC Emulator)
* Windows XP Professional SP2
* VS 2008
*/
/*
The exception
------------------
at System.Net.HttpWebRequest.set_ContentLength(Int64 value)
at System.Net.HttpWebRequest.BufferConnectStream.WritingSucceeds()
at System.Net.HttpWriteStream.doClose()
at System.Net.HttpWriteStream.Finalize()
*/
public static string DoPost(string url)
{
// initialize from variables
string responseString = string.Empty;
ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebResponse response;
byte[] data = encoding.GetBytes("dummy");
StreamReader reader;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
//do the processing
SetRequestProperties(request, "POST"); // SETTING METHOD TO POST HERE
request.GetRequestStream().Write(data, 0, data.Length);
request.GetRequestStream().Close();
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
responseString = reader.ReadToEnd();
//clean up
response.Close();
response.GetResponseStream().Close();
response.GetResponseStream().Dispose();
reader.Close();
reader.Dispose();
reader = null;
response = null;
request = null;
encoding = null;
//return
MessageBox.Show("POST SUCCESS");
return responseString;
}
public static string DoGet(string url)
{
// initialize from variables
string responseString = string.Empty;
ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebResponse response;
byte[] data = encoding.GetBytes("dummy");
StreamReader reader;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
//do the processing
SetRequestProperties(request, "GET"); // SETTING METHOD TO GET HERE
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
responseString = reader.ReadToEnd();
//clean up
response.Close();
response.GetResponseStream().Close();
response.GetResponseStream().Dispose();
reader.Close();
reader.Dispose();
reader = null;
response = null;
request = null;
encoding = null;
//return
MessageBox.Show("GET SUCCESS");
return responseString;
}
private static void SetRequestProperties(HttpWebRequest request, string s)
{
request.Method = s;
request.AllowWriteStreamBuffering = true;
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.SendChunked = false;
request.Credentials = CredentialCache.DefaultCredentials;
request.UserAgent = "my mobile user agent";
request.Timeout = 60000;
request.ProtocolVersion = new System.Version("1.1");
}
你可以得到應用程序的日誌,並將其放在pastebin?使用http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html上給出的配置文件來獲取日誌。日誌會顯示發生了什麼。 – feroze 2010-01-04 16:38:23
嗨feroze。 謝謝。這個日誌記錄工作在緊湊框架上嗎?無論如何,我會嘗試。謝謝。 /cethie – cethie 2010-01-05 15:22:22
OOPS! 來自System.Net的異常來自客戶端(發送請求的移動設備)。使用.NET Compact Framework的應用程序不能有app.config等,因此不知道我是否可以跟蹤:( – cethie 2010-01-05 15:52:42