[英]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.