1

次のエラーがあります。

((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 アクションがあります

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