Calling remote Web service from NAV with .Net Interop Error

kopijakopija Member Posts: 4
edited 2014-12-05 in NAV Three Tier
Hi all,

I have a problem with calling remote Web service from NAV with dotNet interop variables.

I have created local web service that has only one action (HelloWorld).
The following is a sample SOAP 1.1 request and response:
POST /SimpleWebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://localhost/HelloWorld"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorld xmlns="http://localhost" />
  </soap:Body>
</soap:Envelope>

The code that I have written:
dotNetXMLDoc := dotNetXMLDoc.XmlDocument();
dotNetHttpUtility := dotNetHttpUtility.HttpUtility();

XMLRequestString := '<?xml version="1.0" encoding="utf-8"?>';
XMLRequestString += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
XMLRequestString += ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"';
XMLRequestString += ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
XMLRequestString += ' <soap:Body>';
XMLRequestString += ' <HelloWorld xmlns="http://localhost" />';
XMLRequestString += ' </soap:Body>';
XMLRequestString += '</soap:Envelope>';

dotNetWebHeaders := dotNetWebHeaders.WebHeaderCollection();
dotNetHttpWebRequest := dotNetHttpWebRequest.CreateHttp('http://localhost/SimpleWebService.asmx');

dotNetHttpWebRequest.Method('POST');
dotNetHttpWebRequest.ContentType('text/xml');
dotNetHttpWebRequest.MediaType('text/xml');

dotNetWebHeaders.Add('SOAPAction','http://localhost/HelloWorld');
dotNetHttpWebRequest.Headers(dotNetWebHeaders);
dotNetHttpWebRequest.ReadWriteTimeout(30);

dotNetStreamOut := dotNetStreamOut.MemoryStream();
dotNetStreamOut := dotNetHttpWebRequest.GetRequestStream;
dotNetStreamWriter := dotNetStreamWriter.StreamWriter(dotNetStreamOut);
dotNetStreamWriter.Write(XMLRequestString);
dotNetStreamWriter.Close();

dotNetWebResponse :=  dotNetHttpWebRequest.GetResponse;
dotNetStreamIn := dotNetWebResponse.GetResponseStream;
dotNetStreamReader := dotNetStreamReader.StreamReader(dotNetStreamIn);

dotNetXMLDoc.LoadXml(dotNetStreamReader.ReadToEnd());
MESSAGE(dotNetXMLDoc.OuterXml);

On line
dotNetWebResponse :=  dotNetHttpWebRequest.GetResponse;

I get the following error: A call to System.Net.HttpWebRequest.GetResponse failed with this message: The remote server returned an error: (415) Unsupported Media Type.

What am I doing wrong?

Kopija

Comments

  • kopijakopija Member Posts: 4
    This issue has been resolved so I will post the solution for archive :D

    The problem occured because setting the WebHeaderCollection to the HttpWebRequest removed all previously set headers.

    So the "solution" was to set additional headers before setting the ContentType:
    dotNetWebHeaders.Add('SOAPAction','http://localhost/HelloWorld');
    dotNetHttpWebRequest.Headers(dotNetWebHeaders);
    dotNetHttpWebRequest.ContentType('text/xml');
    dotNetHttpWebRequest.MediaType('text/xml');
    
  • JAJJAJ Member Posts: 52
    I have an issue with Web service NAV with dotNet interop.
    I do not know how manage the error with HttpWebRequest.GetResponse sentence. In my environment I have two databases that communicate each other using a web services. If I try to post some document and my web service return an error I want to manage this error and show it to the user but HttpWebRequest.GetResponse sentence return an internal error 500.
    Anybody knows how manage it?

    Thanks in advance.
  • smshydsmshyd Member Posts: 72
    I have tried with above code but I am getting following error
    A call to System.Net.HttpWebRequest.GetResponse failed with this message: The remote server returned an error: (500) Internal Server Error
    is there any thing where i have to change in the above code thanks in advance
  • mouliyadavmouliyadav Member Posts: 9
    edited 2016-10-10
    Hi smshyd, im getting the same response . may i know in the end how did you resolve that error.
    Regards
    Mouli
Sign In or Register to comment.