现在我尝试从Windows服务调用本地服务器中的Web服务,但我有错误 错误是"请求被中止:请求被取消。" 我的代码是
try {
XmlDocument soapEnvelopeXml = new XmlDocument();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("xx.asmx");
request.UserAgent = "Mozilla/5.0";
request.Host = "server";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "\"xx\"");
request.Method = "POST";
request.Accept = "text/xml";
soapEnvelopeXml.LoadXml(getXml(dt));
request.ContentLength = soapEnvelopeXml.OuterXml.Length;
using (Stream Stream = request.GetRequestStream()) {
soapEnvelopeXml.Save(Stream);
}
using (WebResponse response = request.GetResponse()) {
using (StreamReader rd = new StreamReader(response.GetResponseStream())) {
string soapResalt = rd.ReadToEnd();
CtlCommon.CreateErrorLog(strPath, soapResalt);
}
}
} catch (Exception ex) {
CtlCommon.CreateErrorLog(strPath, ex.InnerException.ToString);
CtlCommon.CreateErrorLog(strPath, ex.Message);
}
有段时间我尝试关闭Stream,StreamReader和响应,但错误仍然存在
答案 0 :(得分:0)
可以添加
request.Timeout = 2000
ServicePointManager.DefaultConnectionLimit = 1000
request.KeepAlive = False
request.ProtocolVersion = HttpVersion.Version10
request.ServicePoint.Expect100Continue = False
答案 1 :(得分:0)
我遇到的问题是像Élie这样的法语字母---必须使用ASCII编码。
答案 2 :(得分:0)
我遇到了这个问题,因为发送了挪威的特殊字符(例如Ø,å等)。通过使用UTF-8编码解决了该问题。
使用UTF-8编码发送数据。您可以参考以下C#代码作为UTF-8编码的示例。
httpWebRequest.ContentType = "text/xml; charset=utf-8";
httpWebRequest.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(xmlData); //Convert to UTF-8
httpWebRequest.ContentLength = byteArray.Length; //Set the length of UTF-8 format
//Send the data in UTF-8 format
using (Stream streamWriter = httpWebRequest.GetRequestStream())
{
streamWriter.Write(byteArray, 0, byteArray.Length);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
ret = streamReader.ReadToEnd();
}