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#.
Related
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>
Soap Respose as:
<?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>
<LoginResponse xmlns="http://example.com/SystemIntegration">
<FirstName>#FirstName</FirstName>
<LastName>#LastName</LastName>
</LoginResponse>
</soap:Body>
</soap:Envelope>
I'm trying to read it as:
XDocument doc = XDocument.Parse(strReturnStatus);
List<XElement> result = doc.Elements("LoginResponse").ToList();
for (int intc = 0; intc <= result.Count - 1; intc++)
{
strResponseCode = result[intc].Element("FirstName").Value.ToString();
strResponseText = result[intc].Element("LastName").Value.ToString();
}
But it returning null result.
How to read above respose in asp.net c#??
The easiest thing is to create a proxy class for your service.
You can do that using the 'Add Service Reference' option in Visual Studio. Enter the URL of the service, and Visual Studio will generate the source code for you.
From that point you can access the service using C# code. No need to manually extract the payload of the SOAP message.
Note if you are the implementator of the service: ASMX webservices are deprecated for a long time already. If you can, use WCF.
Use Descendants() method in XDocumentobject to navigate through XML nodes to get elements, you can follow steps provided in this post Using C# to parse a SOAP Response
I have a c# web app that has a simple task: consume an external web service, and loop through its returned xml.
I am consuming the web service like this:
WSExternal.SoapHeaderCredentials objHeaderCredentials = new WSExternal.SoapHeaderCredentials();
objHeaderCredentials.UserName = "username";
objHeaderCredentials.Password = "password";
WSExternal.MembershipAuthenticationService objClient = new WSExternal.MembershipAuthenticationService();
objClient.SoapHeaderCredentialsValue = objHeaderCredentials;
I can call the single exposed web method like this:
objClient.GetMembershipUserList(false);
If I hit the web service through a client like SoapUI or XMLSpy, it returns xml like this:
<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>
<GetMembershipUserListResponse xmlns="http://namespaceofwebservice">
<GetMembershipUserListResult>
<UserList>
<MembershipUserData>
<id>1</id>
<UserName>userone</UserName>
<PrimaryFirstName>User</PrimaryFirstName>
<PrimaryLastName>One</PrimaryLastName>
<Address>13 MAIN STREET</Address>
<City>ROCHESTER</City>
</MembershipUserData>
<MembershipUserData>
<id>2</id>
<UserName>usertwo</UserName>
<PrimaryFirstName>User</PrimaryFirstName>
<PrimaryLastName>Two</PrimaryLastName>
<Address>15 WEST AVE</Address>
<City>JERSEY CITY</City>
</MembershipUserData>
</UserList>
<Page>0</Page>
</GetMembershipUserListResult>
</GetMembershipUserListResponse>
</soap:Body>
My question is, how do I "capture" this xml from the web method, and loop through it to get the relevant data out of the nodes?
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.
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.