1

I am trying to make a soap request:

 var _apiUrl = "https://www1.gsis.gr:443/wsaade/RgWsPublic2/RgWsPublic2";
            using (var httpClient = new HttpClient())
            {
                var response = await httpClient.PostAsync(
                    _apiUrl, new StringContent(
                      "myXML",
                        Encoding.UTF8,
                        "application/soap+xml"));
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(await response.Content.ReadAsStringAsync());
                }
            }

When trying to make the request I am getting error in response

" Exception thrown: 'System.Net.Http.HttpRequestException' in mscorlib.dll."

I have windows 7 64bit, and I am using the .net framework 4.7.2. If I will run the project in windows 10 or if I will downgrade my framework in 4.6.1 then it works fine. Why this is happening? enter image description here

3
  • 1
    Your problem is on content-type "text/xml; encoding='utf-8'" and xml serialization it should like this System.Text.Encoding.ASCII.GetBytes(myXML); Commented Mar 3, 2020 at 9:07
  • Than you Md Farid Uddin Kiron, could you give give me an example? Commented Mar 3, 2020 at 9:37
  • Hi solution provided, hope that would help as expected. Commented Mar 3, 2020 at 9:43

1 Answer 1

1

Its seems you are encountered that error because of wrong xml serialization it should be like "text/xml; encoding='utf-8'" and System.Text.Encoding.ASCII.GetBytes(myXML) instead of what you are trying.

You could try this way:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("RequestURL");
    byte[] bytes;
    bytes = System.Text.Encoding.ASCII.GetBytes("myXML");
    request.ContentType = "text/xml; encoding='utf-8'";
    request.ContentLength = bytes.Length;
    request.Method = "POST";
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(bytes, 0, bytes.Length);
    requestStream.Close();
    HttpWebResponse response;
    response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        string responseStr = new StreamReader(responseStream).ReadToEnd();
        return responseStr;
    }

You could refer Official Document

Hope that would help you.

Sign up to request clarification or add additional context in comments.

8 Comments

I am still getting the same issue
How is your xml include that on your question as well.
You cannot pass xml that way, you have to convert in to C# class, then need to serialize it. finally have to pass that on above request have a look here
I am using XmlDocument load and then doc.OuterXml
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.