现在我尝试从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);
}
有时我尝试关闭流,流读器和响应,但错误仍然存在
我遇到了问题,因为发送了挪威特殊字符(例如Ø,Å等(。通过使用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();
}
我遇到的问题是像élie这样的法语字母---必须使用ascii编码。