目前我使用以下方法将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);
}
}
答案 0 :(得分:0)
谢谢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();
}