1

我有以下错误:

((MS.Internal.InternalWebRequestStream)requestStream).ReadTimeout (and 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 操作

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));
    }
}
4

0 回答 0