Ibrahim Alsurkhi
Ibrahim Alsurkhi

Reputation: 113

The request was aborted: The request was canceled

now i try to call web service in local server from windows service but i have error the error is "The request was aborted: The request was canceled." my code is

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);
}

some time i try to close Stream, StreamReader and response but the error still exist

Upvotes: 0

Views: 7489

Answers (2)

Aman
Aman

Reputation: 41

I faced the issue because of the Norwegian special characters (e.g. Ø, å, etc.) being sent across. Resolved it by using the UTF-8 encoding.

Send the data using the UTF-8 encoding. You can refer to the following C# code as sample for UTF-8 encoding.

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();
}

Upvotes: 4

kiev
kiev

Reputation: 2070

The issue I had was french letters like Élie --- had to use ASCII encoding.

Upvotes: 0

Related Questions