我有以下错误:
((MS.Internal.InternalWebRequestStream)requestStream).ReadTimeout(和WriteTimeout)抛出了System.InvalidOperationException类型的异常
在MSDN上说:
应重写WriteTimeout属性以为流提供适当的行为。如果流不支持超时,则此属性应引发InvalidOperationException。
所以我想明白我应该做些什么才能避免这种异常
这是我的代码。在点击事件上我有:
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
th = new Thread(StartSaveDataSource);
th.Start();
});
这是我的函数将xml中的数据发送到php
private void StartSaveDataSource()
{
Uri uri;
String str = String.Empty;
str = @"http://example.com/new/index.php?action=getmongopod";
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Model));
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, dataSource);
}
using (StringWriter stringWriter = new StringWriter())
{
serializer.Serialize(stringWriter, dataSource);
xmlString = stringWriter.GetStringBuilder().ToString().Replace(@"encoding=""utf-16""", @"encoding=""utf-8""");
}
Uri strReq;
bool isStrReqGained = false;
if (Application.Current.IsRunningOutOfBrowser)
{
strReq = new Uri(str, UriKind.Absolute);
isStrReqGained = true;
}
else
{
Uri docUri = HtmlPage.Document.DocumentUri;
if (docUri == null || String.IsNullOrWhiteSpace(docUri.AbsolutePath))
{
strReq = null;
}
else
{
strReq = new Uri(docUri, SilverlightApplication2.Resources.Urls.GetMongoPod);
isStrReqGained = true;
}
}
if (isStrReqGained)
{
WebRequest webRequest = WebRequest.Create(strReq);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.BeginGetRequestStream(CreateCustomRequest, webRequest);
}catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
});
}
这是asyncCallback Action
private void CreateCustomRequest(IAsyncResult asyncResult)
{
try
{
WebRequest request = (WebRequest) asyncResult.AsyncState;
Stream requesStream = null;
try
{
requesStream = request.EndGetRequestStream(asyncResult);
using (StreamWriter writer = new StreamWriter(requesStream))
{
requesStream = null;
writer.Write("xmlstring=" + HttpUtility.UrlEncode(xmlString));
}
}
catch(Exception ex)
{
DispatcherHelper.CheckBeginInvokeOnUI(() => MessageBox.Show(ex.ToString()));
}
finally
{
if (requesStream != null)
{
requesStream.Close();
}
}
request.BeginGetResponse(ReadResponse, request);
}
catch (Exception ex)
{
DispatcherHelper.CheckBeginInvokeOnUI(() => MessageBox.Show(ex.Message));
}
}