Asked by:
SOAP web request

Question
-
User580518845 posted
I am using this link to create the soap envelop.
http://www.roelvanlisdonk.nl/?p=1893But when I reach request.GetResponse(), I get error."The remote server returned an error: (500) Internal Server Error."
I think the error is because of the
webRequest.Headers.Add(@"SOAP:Action"), do I need to add any name space etc to the soap action?public HttpWebRequest CreateWebRequest() { string soapAction =@"SOAP:Action"; // is this correct? HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://myServiceURL/services"); webRequest.Headers.Add(@"SOAP:Action"); webRequest.ContentType = "text/xml;charset=\"utf-8\""; //webRequest.ContentType = "text/xml;charset=\"utf-8\";action=\""+ soapAction + "\""; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; }
Sunday, June 16, 2013 9:35 AM
All replies
-
User220959680 posted
Jaypal
using this link to create the soap envelop.
http://www.roelvanlisdonk.nl/?p=1893Example shown in the above referred article is accessing SOAP based Web Service.
Jaypal
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://myServiceURL/services")
You are accessing a RESTFul service (which is evident from the URI provided).
Refer Consuming Restful Service section at http://www.codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in-NET-Framewor
EDIT:-
Note that HTTP POST verb is used to create new resource(i.e., new data record in database) in RESTFul service.string soapAction =@"SOAP:Action"; // is this correct?
No. This is not requried. Refer amended source below.public HttpWebRequest CreateWebRequest() { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://myServiceURL/services"); webRequest.Method = "POST"; WebRequest.ContentType = @"application/xml; charset=utf-8"; return webRequest; }
Note:- when the service is protected(security), client (above source) needs to provide valid credentials.
It is expected that above method is called in another operation to create a request to the service. then process the response received from the service.Examplestatic void Main(string[] args) { //calling the above operation that creates service request CreateWebRequest(); //POST data to the service using //receive the service response || similar to the one below. HttpWebResponse resp = req.GetResponse() as HttpWebResponse; Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription)); Console.Read(); }
Sunday, June 16, 2013 5:19 PM -
User580518845 posted
Thank you for the reply.
I try adding these two lines
webRequest.Method = "POST";
WebRequest.ContentType = @"application/xml; charset=utf-8";
still I get the same error.
in the code below, Yes I use the https://
I added the userid and password in the xml file of this line
soapEnvelopeXml.LoadXml(@"<soap:Envelope xmlns:soap...
and do you see anything wrong in the link http://www.roelvanlisdonk.nl/?p=1893?
The only difference I can see is, they use
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <HelloWorld3 xmlns=""http://tempuri.org/"">
and my xmlfile does not have this header section below
<?xml version=""1.0"" encoding=""utf-8""?>
webservice gave response back for the request XML I created when I tried with "SOAPUI" program.
So I guess something is wrong with my header file to create the soapenvelope.
Sunday, June 16, 2013 10:01 PM -
User580518845 posted
I just tried adding the <?xml version=""1.0"" encoding=""utf-8""?> to the
soapEnvelopeXml.LoadXml, still I get the same 500 error.
a)
my wsdl have a section like this blow. So I thought I need a the soapaction in the request header with the URI
<soap:operation
soapAction="http://xmldefs.bcd.sa.shs/Common/V1/FindsearchInventory" />
<wsdl:input>
b) Here is the complete code.Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Reference: http://www.roelvanlisdonk.nl/?p=1893 Dim request As HttpWebRequest = CreateWebRequest() Dim soapEnvelopeXml As New XmlDocument() 'soapEnvelopeXml.LoadXml("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" ..../soapenv:Envelope>") soapEnvelopeXml.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?><soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" ......<wsse:UsernameToken><wsse:Username>XXXXXX</wsse:Username> <wsse:Password>YYYYYY</wsse:Password>.......</soapenv:Body></soapenv:Envelope>") ' Using stream As Stream = request.GetRequestStream() soapEnvelopeXml.Save(stream) End Using Using response As WebResponse = request.GetResponse() Using rd As New StreamReader(response.GetResponseStream()) Dim soapResult As String = rd.ReadToEnd() Console.WriteLine(soapResult) End Using End Using End Sub Public Function CreateWebRequest() As HttpWebRequest Dim soapAction As String = "http://xmldefs.bcd.sa.shs/Common/V1/FindsearchInventory" Dim webRequest2 As HttpWebRequest = DirectCast(WebRequest.Create("https://Mywebervices"), HttpWebRequest) ' webRequest2.ContentType = "application/soap+xml;charset=UTF-8;action=""" + soapAction + """" webRequest2.ContentType = "application/xml;charset=UTF-8;" webRequest2.Method = "POST" 'webRequest2.Headers.Add("Cache-Control: no-Cache") 'webRequest2.Credentials = New NetworkCredential("xxxx", "yyyyy") 'userid and password Return webRequest2 End Function
Please tell me what is wrong with the code.
Sunday, June 16, 2013 10:34 PM