簡體   English   中英

HttpWebRequest POST方法異常

[英]HttpWebRequest POST method Exception

目前,我正在使用以下方法將xml文件內容發布到Extranet網站。 我每10秒創建一個新線程以同時發布多個文件。

在這種情況下,我會收到許多文件的異常: System.Net.WebException:操作已超時

盡管會引發此異常,但仍有一些異常文件的內容發布在網站上。

1)即使某些文件存在超時異常,請求張貼的行為是否正確?

2)如何處理諸如某些異常文件的內容而不是其他異常文件的場景? 我如何確定哪些內容不會發布,以便嘗試重新發布?

3)歡迎提出其他更好實施的建議。

public void SendMessage(XmlDocument doc) {
    var httpWReq = (HttpWebRequest)WebRequest.Create(_extranetRemitService);

    httpWReq.Proxy = WebRequest.GetSystemWebProxy();
    httpWReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
    httpWReq.UseDefaultCredentials = true;
    // encode xml file contents (melFileContents is a string)
    var melMessage = doc.InnerXml;
    byte[] data = Encoding.ASCII.GetBytes(melMessage);
    //post the xml data as text/xml content type
    httpWReq.Method = "POST";
    httpWReq.ContentType = "text/xml;charset=utf-8";
    httpWReq.ReadWriteTimeout = -1;
    httpWReq.KeepAlive = false;
    httpWReq.ContentLength = data.Length;
    using (Stream stream = httpWReq.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }
}

感謝crackhaus和Jim Mischel。 下面是工作示例:

public void SendMessage(XmlDocument doc)
    {
        var httpWReq = (HttpWebRequest)WebRequest.Create(_extranetRemitService);
        httpWReq.Proxy = WebRequest.GetSystemWebProxy();
        httpWReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
        httpWReq.UseDefaultCredentials = true;

        // encode xml file contents (melFileContents is a string)
        var melMessage = doc.InnerXml;
        byte[] data = Encoding.ASCII.GetBytes(melMessage);

        //post the xml data as text/xml content type
        httpWReq.Method = "POST";
        httpWReq.ContentType = "text/xml;charset=utf-8";
        httpWReq.ReadWriteTimeout = 10000;
        httpWReq.KeepAlive = false;
        httpWReq.ContentLength = data.Length;
        using (Stream stream = httpWReq.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
            stream.Close();
        }

        var response = (HttpWebResponse)httpWReq.GetResponse();
        response.Close();


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM