How to request and get the response in soap web services c# - c#

I have the .asmx url that i need to get the order details by order number.
My problem is that when i execute the code it bring back the html design of 'http://domain.co.za/services/portal.asmx?op=GetOrderDefinition' without populating the response.
here is my code
var client = new RestClient("http://domain.co.za/services/portal.asmx?op=GetOrderDefinition");
var requests = new RestRequest(Method.GET);
client.Proxy = WebRequest.DefaultWebProxy;
client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
requests.AddHeader("Host", "test.lensportal.co.za");
requests.AddHeader("Content-Type", "text/xml; charset=utf-8");
requests.AddHeader("SOAPAction", "http://www.domain.co.za/GetOrderDefinition");
requests.AddParameter("{\r\n \"orderRef\": \"" + LabOrderReference + "\"\r\n}", RestSharp.ParameterType.RequestBody);
IRestResponse responses = client.Execute(requests);
The link 'http://domain.co.za/services/portal.asmx?op=GetOrderDefinition' contain samples below and i need to pass 'orderRef' when requesting and get back list off order details. Please note that the link has also SOAP 1.1 design for response for now i just included the request part.
POST /services/portal.asmx HTTP/1.1
Host: test.lensportal.co.za
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.domain.co.za/GetOrderDefinition"
<?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>
<GetOrderDefinition xmlns="http://www.domain.co.za">
<orderRef>string</orderRef>
</GetOrderDefinition>
</soap:Body>
</soap:Envelope>

Related

Sending a SOAP request to a web service using C#?

I am new to C# and my supervisor wants me to send a SOAP request to a web service and receive the connection string in C#. I have worked with Python before but I am really confused as how to do it in C#. This is the Python code I wrote. I am really lost.
import requests
url="http://172.20.3.3:8250/GS/GetConnectionStrings.asmx?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?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>
<DatabaseConnectionString xmlns='http://tempuri.org/'>
<DatabaseName>LMA</DatabaseName>
</DatabaseConnectionString>
</soap:Body>
</soap:Envelope>"""
response = requests.post(url,data=body,headers=headers)
print response.content
I need to convert the above code to C#.

WCF Client cant read "mutlipart/reoated"

I have a simpel WCF-Client.
Binding
var binding = new WSHttpBinding(SecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
So now my Problem. The server respond with somthing like this:
--MIMEBoundary_4c0931c2662c48df1de58a689b82ce1eb3373535d903393a
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-ID: <0.5c0931c2662c48df1de58a689b82ce1eb3373535d903393a#apache.org>
<?xml version="1.0" encoding="UTF-8"?>
<--rest of the response xml-->
--MIMEBoundary_4c0931c2662c48df1de58a689b82ce1eb3373535d903393a--
It looks like the normal respond is simply packed in the MIMEBoundary. I try to switch to MTOM. But i got this Error:
Error creating a reader for the MTOM message.
MTOM messages must have type 'application/xop+xml'
What could i do?

SOAP API with DomainBox in ASP.NET application

I am trying to integrate an ASP.NET application to http://www.domainbox.com/ using their SOAP API. I couldn't find any examples. The only thing they offer is the SOAP Resquest and Response examples. My question is how do I call the request and how to I get the response data to use in my application?
REQUEST:
<soap12:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap12=”http://www.w3.org/2003/05/
soap-envelope”>
<soap12:Body>
<QueryDomainRenewalSettings xmlns=”https://sandbox.domainbox.net/”>
<AuthenticationParameters>
<Reseller>myreseller</Reseller>
<Username>myusername</Username>
<Passwordmy>password</Password>
</AuthenticationParameters>
<CommandParameters>
<DomainName>atestdomain.co</DomainName>
</CommandParameters>
</QueryDomainRenewalSettings>
</soap12:Body>
</soap12:Envelope>
RESPONSE:
<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.
w3.org/2001/XMLSchema”>
<soap:Body>
<QueryDomainRenewalSettingsResponse xmlns=”https://sandbox.domainbox.net/”>
<QueryDomainRenewalSettingsResult>
<ResultCode>100</ResultCode>
<ResultMsg>Domain Renewal Settings Queried Successfully</ResultMsg>
<TxID>4d76201a-3b6d-4ccc-bca5-729439bbac9b</TxID>
<DomainId>87967</DomainId>
<AutoRenew>true</AutoRenew>
<AutoRenewDays>60</AutoRenewDays>
</QueryDomainRenewalSettingsResult>
</QueryDomainRenewalSettingsResponse>
</soap:Body>
</soap:Envelope>
Thanks.
You should add the Domainbox API as a service reference if you're using ASP.NET.
That will let you use it just like a normal C#/VB class,
var parameters = new Domainbox.CheckDomainAvailabilityParameters {DomainName= "example.com"};
var result = apiObject.CheckDomainAvailiability(authObject, paramters);
if (result.ResultCode == 100) { // get results... }
I can provide more detailed example if required.

Passing XML document as an parameter to Web services using C#

I have to sent the XML document as an parameter to request an WebRequest using the Post method and get response. Web service implements the following method:
public string Register(XmlDocument register){...}
I'm trying doing like this , but I can't get response and I'm not sure that my code is working =(
HttpWebRequest request = HttpWebRequest.Create("http://ws2.sti.gov.kg/TRKService/PatentService.asmx/Register") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Encoding ex = Encoding.GetEncoding("iso-8859-1");
XmlDocument doc = new XmlDocument();
doc.LoadXml("<foo><bar>baz</bar></foo>");
string rawXml = doc.OuterXml;
string requestText = string.Format("register={0}", HttpUtility.UrlEncode(rawXml, ex));
Stream requestStream = request.GetRequestStream();
StreamWriter requestWriter = new StreamWriter(requestStream, ex);
requestWriter.Write(requestText);
requestWriter.Close();
Maybe someone has a working example?
403 Error
If your getting a 403 when trying to import the web service this may not be your fault. Try
looking at the wsdl file in your web browser. If you still get the 403 error then their is no use coding any further because you don't have permission to use that service.
Code Syntax
Also, in your code I it appears your not reading back the response anywhere. Your last statement writes the XML to the stream but your not reading back the response anywhere.
requestWriter.Write(requestText);
requestWriter.Close();
SOAP
If the web service your are communicating with is SOAP based then your XML payload needs to conform to the SOAP standard. Your sample code above uses very basic XML, probably because it's just an example, but for it to work you will need requests with a format along the lines of
<?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>
<GetWeather xmlns="http://www.webserviceX.NET">
<CityName>string</CityName>
<CountryName>string</CountryName>
</GetWeather>
</soap:Body>
</soap:Envelope>
Not
<foo><bar>baz</bar></foo>
Again, you've obviously only used foo for an example but this could also be the source of your problem so inspect the actual XML payload you are sending.

Getting MIME type from Web Service Response header

I invoke a web service provided by an external partner company. The web service returns files (.pdf, .dox, .png, ...) as a byte array.
If I would need to get the Header information (in detail I am interested in the content-type data) from the code, how can I get this information?
On our side, we are using VS 2010 and C# as language.
Here the code:
var client = new PublicService();
wsRequest request = new wsRequest();
var docInfo = new documentInfo();
docInfo.documentId = HSdocumentID;
docInfo.position = 1;
request.documentInfos = { docInfo };
byte[] doc = client.deliver(deliverRequest); //returns the file as byte array
The RESPONSE header would look like:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:deliverResponse xmlns:ns2="http://www.dalle.com/webservices/record/2012a">
<return>
<Include xmlns="http://www.w3.org/2004/08/xop/include"
href="cid:d3a#example.jaxws.sun.com"/>
</return></ns2:deliverResponse></S:Body></S:Envelope>
Content-Id: <d3a#example.jaxws.sun.com>
Content-Transfer-Encoding: binary
Content-Type: application/pdf <-- THIS IS THE INFO I NEED TO GET
Check out if there are SOAP headers on the Web Method call that answer your question
On the web method I do not have any properties/attributes that refer to the header. Is there a general way to get the Response header or is the web service that should provide features to get it?
(I provided an answer, rather than a comment, due to the code to be copied)

Categories