we are receiving a SOAP request and we can't modify it all. So now we have to change our webservice to fit in with the call. Im pretty much there, expect the prefix for their namespace is different to the one that gets generated and it doesn't hit the method unless its the same:
Here is my Webservice:
[WebService(Namespace = "http://domain.co.za/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
public class MyWebservice : System.Web.Services.WebService
{
[WebMethod]
public string PushMessage(object payload)
{
}
}
And the SOAP they are sending:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://domain.co.za/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns0:username soapenv:actor=""></ns0:username>
<ns0:password soapenv:actor=""></ns0:password>
</soapenv:Header>
<soapenv:Body>
<ns0:PushMessage>
<type>confirmation</type>
<autoRelease>true</autoRelease>
<payload>
<Confirmation>
<MessageEnvelope>
.
.
</MessageEnvelope>
</Confirmation>
</payload>
</ns0:PushMessage>
</soapenv:Body>
</soapenv:Envelope>
As you can see they are using ns0, the one that gets generates by the WDSL is web, is there anyway to force my namespace to generate as ns0: instead of the default web:
And is there a way to remove the ns0/web from the payload element? They are not sending the namespace with the payload, but the WDSL generates it as web:payload
Thank you.
Try regex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace Calendar
{
class Program
{
static void Main(string[] args)
{
string input =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://domain.co.za/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<soapenv:Header>" +
"<ns0:username soapenv:actor=\"\"> </ns0:username>" +
"<ns0:password soapenv:actor=\"\"> </ns0:password>" +
"</soapenv:Header>" +
"<soapenv:Body>" +
"<ns0:PushMessage>" +
"<type>confirmation </type>" +
"<autoRelease>true </autoRelease>" +
"<payload>" +
"<Confirmation>" +
"<MessageEnvelope>" +
"</MessageEnvelope>" +
"</Confirmation>" +
"</payload>" +
"</ns0:PushMessage>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
string pattern = "(</?)(\\w:)(\\w)";
Regex expr = new Regex(pattern);
input = expr.Replace(input, "$1$3");
XDocument doc = XDocument.Parse(input);
}
}
}
Related
I am try to get session ID from soapenv using RestClient but i get value as 0 or -1.
This is my code
var client = new RestClient(url);
client.Authenticator = new HttpBasicAuthenticator(name, pwd);
var request = new RestRequest("/{sessionID}",Method.GET);
var response = client.Get(request);
var content = response.Content; // Raw content as string
Console.WriteLine(response.ContentLength);
My Soapenv looks like this
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v7="urn://oracle.bi.webservices/v7">
<soapenv:Header/>
<soapenv:Body>
<v7:logon>
<v7:name>pxxxx</v7:name>
<v7:password>Pxxxx</v7:password>
</v7:logon>
</soapenv:Body>
</soapenv:Envelope>
I am trying to get Session ID (9insv92nke9lig83bqd5pu5tqt7kp8u0toe9l) that looks likes this
<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" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:sawsoap="urn://oracle.bi.webservices/v7">
<soap:Body>
<sawsoap:logonResult>
<sawsoap:sessionID xsi:type="xsd:string">9insv92nke9lig83bqd5pu5tqt7kp8u0toe9l</sawsoap:sessionID>
</sawsoap:logonResult>
</soap:Body>
</soap:Envelope>
Not able to figure out the how to get the session ID. Can anyone help?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApp1
{
class Program
{
const string FILENAME = #"c:\Temp\Test.xml";
static void Main(string[] args)
{
string content = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(content);
XElement xSessionID = doc.Descendants().Where(x => x.Name.LocalName == "sessionID").FirstOrDefault();
string sessionID = (string)xSessionID;
}
}
}
I'm doing LoadXML, but I need to add a field from the form but I couldn't,
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Servis5001>
<Kodu>abcdefg</Kodu>
<Sifre>123456789</Sifre>
<HesKodu>TXBHesKodu.Text</HesKodu>
</Servis5001>
</soap:Body>
</soap:Envelope>");
return soapEnvelopeDocument;
I need to add the TXBHESKodu.Text from the form to the <HesKodu> field here.
I think I couldn't make the upper quotation marks added into the file.
Can you show me how to do it?
Here are two ways you could do that. The first being the simplest. Just use string formatting like this (note the $ sign before your # sign):
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
$#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Servis5001>
<Kodu>abcdefg</Kodu>
<Sifre>123456789</Sifre>
<HesKodu>{TXBHesKodu.Text}</HesKodu>
</Servis5001>
</soap:Body>
</soap:Envelope>");
The second method uses the XML Dom to add the text into the element. We find the element using the XPath syntax:
var xmlDocument = GetXmlDocument(#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Servis5001>
<Kodu>abcdefg</Kodu>
<Sifre>123456789</Sifre>
<HesKodu></HesKodu>
</Servis5001>
</soap:Body>
</soap:Envelope>");
XmlNode HesKodu = xmlDocument.SelectSingleNode("//Servis5001/HesKodu");
HesKodu.InnerText = TXBHesKodu.Text;
You would be better off letting Visual Studio handle all the SOAP stuff for you. Take a look at adding a reference to a SOAP Web Service Reference (right click on project and Add => Web Service Reference). You can simply enter the url with ?wsdl at the end and it will generate everything you need to consume the web service.
You can use the url in a webbrowser too. If you have access to the service you can simply enter the url in the addressbar and press Enter. It should give you a description of the service and how to consume it.
Method Usage Error When I Want to Add TXT HesKodu.Text
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
$#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Servis5001>
<Kodu>abcdef</Kodu>
<Sifre>abcdef</Sifre>
<HesKodu>{TXBHesKodu.Text}</HesKodu>
</Servis5001>
</soap:Body>
</soap:Envelope>");
return soapEnvelopeDocument;
}
SOAP Whatever I Use My Blog Is All The Code Is As Below, Maybe If You Want To See It
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace TOBBHesKoduSorgulama
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BTNSorgula_Click(object sender, EventArgs e)
{
var _url = "https://kpsoda.tobb.org.tr/hesservis.php?wsdl";
var _action = "https://kpsoda.tobb.org.tr/hesservis.php?op=Servis5001";
var result = "";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
result = soapResult;
Console.Write(soapResult);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
$#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Servis5001>
<Kodu>abcdef</Kodu>
<Sifre>abcdef</Sifre>
<HesKodu>{TXBHesKodu.Text}</HesKodu>
</Servis5001>
</soap:Body>
</soap:Envelope>");
return soapEnvelopeDocument;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
}
}
I also tried the Service References Process But I Didn't Know How To Request
string Kodu = "Kodu";
string Sifre = "Sifre";
string HesKodu = "HesKodu";
TOBBHesKoduSorgulamaServices.Servis5001Request S5001R = new TOBBHesKoduSorgulamaServices.Servis5001Request();
S5001R.Kodu = Kodu;
S5001R.Sifre = Sifre;
S5001R.HesKodu = HesKodu;
TOBBHesKoduSorgulamaServices.Servis5001Response S5001Response = ?
Which One Is Right For Me?
Waiting for your support, good work ...
I have Soap web service https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl. This has a method called process. My question is how do I create soap client to send a request, that submits/Retrieves information from the server. The example XML for the request is shown below:
<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
<Request>
<Transaction>
<VersionNumber>1.0</VersionNumber>
<Number>434252-342234-6765</Number>
<SystemIdentifier>SYSTEM999</SystemIdentifier>
<DestinationCode>APACE</DestinationCode>
<ClientCountryCode>ZA</ClientCountryCode>
<Timestamp TimeZone="Africa/Johannesburg">20160601123456</Timestamp>
<TestIndicator>Y</TestIndicator>
<User>ProviderX/Jane Doe</User>
</Transaction>
<MembershipLookup>
<Funder>AFunder</Funder>
<WithMembershipNumber>
<MembershipNumber>123456789</MembershipNumber>
</WithMembershipNumber>
</MembershipLookup>
</Request>
</Member>
The response message is structured as follows :
<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
<Response>
<Transaction>
<VersionNumber>1.0</VersionNumber>
<Number>434252-342234-6765</Number>
<Status>S</Status>
<Timestamp TimeZone="Africa/Johannesburg">20160601123500</Timestamp>
</Transaction>
<Membership NumberOfBeneficiaryRecords="2">
<Funder>AFunder</Funder>
<MembershipNumber>123456789</MembershipNumber>
<Beneficiary SequenceNumber="1">
<DependentCode>00</DependentCode>
<Type>P</Type>
<Status>A</Status>
<BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
</Beneficiary>
<Beneficiary SequenceNumber="2">
<DependentCode>01</DependentCode>
<Type>D</Type>
<Status>A</Status>
<BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
</Beneficiary>
</Membership>
</Response>
</Member>
Please help I am really stuck, Haven't used SOAP services with C#, I have tried examples online but the structure does not follow my message structures are more complicated
The actual message structure is structured as follows:
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:apac="http://apace.systems/apacewebservices/"
xmlns:urn="urn:apace:member:format:1.1">
<soap:Header>
<apac:secureToken>Token66657752</apac:secureToken>
</soap:Header>
<soap:Body>
<apac:process>
<apac:request>
<Member
xmlns="urn:apace:member:format:1.1">
<Request>
<Transaction>
<VersionNumber>1.1</VersionNumber>
<Number>30074</Number>
<SystemIdentifier>LIFEHEALTH</SystemIdentifier>
<DestinationCode>HEALTH263</DestinationCode>
<ClientCountryCode>ZA</ClientCountryCode>
<Timestamp TimeZone="Africa/Johannesburg">20200705123456</Timestamp>
<TestIndicator>Y</TestIndicator>
<User>CIMSZW/Jane Doe</User>
</Transaction>
<MembershipLookup>
<IncludeDetail>Y</IncludeDetail>
<Funder>CIMSZWA</Funder>
<WithMembershipNumber>
<MembershipNumber>11117374</MembershipNumber>
<DependentCode>00</DependentCode>
</WithMembershipNumber>
</MembershipLookup>
</Request>
</Member>
</apac:request>
</apac:process>
</soap:Body>
</soap:Envelope>
Hw do I create the SOAP envelope and the Header part with the Security Key Token
You have two options
Add as service reference , it will genrate the
use Visual studio developer prompt and consume the service using a proxy class
svcutil.exe https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl /t:code /n:*,SampleNamespace /o:C:\Service\sampleServiceProxy.cs /config:C:\Service\sampleService.config /ct:System.Collections.Generic.List`1
You can use a couple different system libraries to create an HttpWebRequest, create custom xml, and insert that xml into your request before you send it. (system.net, system.xml.linq, system.io)
I was able to hit your web service but got a 500 error. (Hopefully you see a log or that wasn't yours!)
Here's a simple class that can call a SOAP web service. It's almost the xml that you need but might need some tweaking. If you're having issues making custom xml this may be a possible solution.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApp1
{
/// <summary>
/// Random class
/// </summary>
class Class1
{
/// <summary>
/// Function that calls a SOAP web service
/// </summary>
public void CallSOAP()
{
try
{
// Construct http post request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://test-submit.health263.systems:8081/apacewebservices/AMF1_0"));
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";
// Setting a namespace for your xml
// I'm not sure the timezone one is set up properly
XNamespace soap = "urn:apace:member:format:1.0";
XNamespace timezone = "TimeZone=\"Africa/Johannesburg\"";
// This constructs your xml using the LINQ library. I modeled after your demo, but needs tweaking as I get 500 error from server.
XElement requestXML =
new XElement(soap + "Member",
new XElement("Request",
new XElement("Transaction",
new XElement("VersionNumber", "1.0"),
new XElement("Number", "434252 - 342234 - 6765"),
new XElement("SystemIdentifier", "SYSTEM999"),
new XElement("DestinationCode", "APACE"),
new XElement("ClientCountryCode", "ZA"),
new XElement(timezone + "Timestamp", "20160601123456"),
new XElement("TestIndicator", "Y"),
new XElement("User", "ProviderX/Jane Doe")
),
new XElement("MembershipLookup",
new XElement("Funder", "AFunder"),
new XElement("WithMembershipNumber",
new XElement("MembershipNumber", 123456789)
)
)
)
);
// Convert the xml into a stream that we write to our request
byte[] bytes = Encoding.UTF8.GetBytes(requestXML.ToString());
request.ContentLength = bytes.Length;
using (Stream putStream = request.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}
// Execute the request and get an xml response "reader". You can read all xml at once or line by line
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var streamData = reader.ReadToEnd();
}
}
catch (Exception ex)
{
// Write exception to console & wait for key press
Console.WriteLine(ex.Message + ex.StackTrace);
Console.ReadKey();
}
}
}
}
I've asked this on the AWS Forums but getting plenty of views but no comments, I wonder if anyone here can shed any light on it?
Hi,
I've been trying to write a simple c# console application to call the topsites service for two days now and still get issues with the signature generation.
I've tested using a java sample in the gallery and can successfully query using my accesskeyid and secret. I've then used my C# code to prove I can generate the same signature and my code will do so, however when I then craft a request and issue it against the api every single one returns a 403 status - signaturedoesnotmatch - please can someone help me find out what the issue is? I'm tearing my hair out with this.
C# Code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace ConsoleApplication1
{
class Program
{
private static string baseUrl = ConfigurationManager.AppSettings["baseUrl"];
private static string accessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
private static string accessKey = ConfigurationManager.AppSettings["accessKey"];
private static string serviceVersion = ConfigurationManager.AppSettings["serviceVersion"];
static void Main(string[] args)
{
HttpClient client = new HttpClient();
string requestParameters = "AWSAccessKeyId=" + accessKeyId + "&Action=TopSites&Count=10&CountryCode=&ResponseGroup=Country&SignatureMethod=HmacSHA256&SignatureVersion=2&Start=1001&Timestamp=" + Amazon.Util.AWSSDKUtils.FormattedCurrentTimestampISO8601;
var signature = generateSignature(requestParameters);
var url = "http://" + baseUrl + "?" + requestParameters + "&Signature=" + signature;
HttpResponseMessage message = client.GetAsync(url).Result;
Console.ReadKey();
}
private static string generateSignature(string queryParameters)
{
string stringToSign = "GET\n" + baseUrl + "\n/\n" + queryParameters;
var bytesToSign = Encoding.UTF8.GetBytes(stringToSign);
var secretKeyBytes = Encoding.UTF8.GetBytes(accessKey);
var hmacSha256 = new HMACSHA256(secretKeyBytes);
var hashBytes = hmacSha256.ComputeHash(bytesToSign);
var signature = System.Net.WebUtility.UrlEncode(Convert.ToBase64String(hmacSha256.Hash));
Trace.Write("String to sign:{0}", signature);
return signature;
}
}
}
Request generated (from Fiddler):
GET http://ats.amazonaws.com/?AWSAccessKeyId=REMOVED&Action=TopSites&Count=10&CountryCode=&ResponseGroup=Country&SignatureMethod=HmacSHA256&SignatureVersion=2&Start=1001&Timestamp=2014-11-20T16:57:52.422Z&Signature=vdKOQYRmoJJL3ecY9GAzmGKHAXevoli6rGcEotGFaNY%3D HTTP/1.1
Host: ats.amazonaws.com
Connection: Keep-Alive
Response:
HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Thu, 20 Nov 2014 16:57:52 GMT
16d
SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.84291dc8-a35e-7dc3-7cc1-56fe20b5b236
0
Based on Darrel's comment and extensive comparisons between requests from the Java app and my sample app I've been able to correctly query the services using a number of requests including the sample one above. It would appear to have been a problem whereby the request string which is signed had an erroneous space character in front of the hostname, for added resiliency I am using the Amazon AWS SDK for .Net to perform the Url Encoding against their requirements to ensure the encoding is correct.
Here's the working sample code:
using System;
using System.Configuration;
using System.Diagnostics;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static string baseUrl = ConfigurationManager.AppSettings["AlexaServiceUrl"];
private static string accessKeyId = ConfigurationManager.AppSettings["AlexaAccessKeyId"];
private static string accessKey = ConfigurationManager.AppSettings["AlexaAccessKey"];
private static string serviceVersion = ConfigurationManager.AppSettings["AlexaServiceVersion"];
static void Main(string[] args)
{
HttpClient client = new HttpClient();
string requestParameters = "AWSAccessKeyId=" + accessKeyId + "&Action=TopSites&Count=10&CountryCode=&ResponseGroup=Country&SignatureMethod=HmacSHA256&SignatureVersion=2&Start=1001&Timestamp=" + Amazon.Util.AWSSDKUtils.UrlEncode(Amazon.Util.AWSSDKUtils.FormattedCurrentTimestampISO8601, false);
var signature = generateSignature(requestParameters);
var url = "http://" + baseUrl + "/?" + requestParameters + "&Signature=" + signature;
HttpResponseMessage message = client.GetAsync(url).Result;
Console.ReadKey();
}
private static string generateSignature(string queryParameters)
{
string stringToSign = String.Format("GET{0}{1}{2}/{3}{4}", "\n", baseUrl, "\n", "\n", queryParameters);
var bytesToSign = Encoding.UTF8.GetBytes(stringToSign);
var secretKeyBytes = Encoding.UTF8.GetBytes(accessKey);
var hmacSha256 = new HMACSHA256(secretKeyBytes);
var hashBytes = hmacSha256.ComputeHash(bytesToSign);
var signature = Amazon.Util.AWSSDKUtils.UrlEncode(Convert.ToBase64String(hmacSha256.Hash), false);
Trace.Write("String to sign:{0}", signature);
return signature;
}
}
}
Hope this helps someone else too.
I would like to send a SOAP Message to a Web Service and read the response. My code is as follows: I will appreciate your help.
I hope my question is not repeated, I have looked around for a solution however I have not been successful.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Xml;
using System.Net;
using System.IO;
namespace TolunaPush
{
public partial class _Default : System.Web.UI.Page
{
private string sourceID = "50001255";
private string email = "adsvine#gmail.com";
private string firstName = "Muz";
private string lastName = "Khan";
private string countryID = "2000077";
private string countryLanguage = "2000240";
private string postalCode = "N19 3NU";
private string dob = "1977-03-08";
private string gender = "2000247";
protected void Page_Load(object sender, EventArgs e)
{
sendSoapMessage();
}
protected void sendSoapMessage()
{
XmlDocument doc = new XmlDocument();
doc.InnerXml = #"<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>
<SubmitPanelist xmlns=""http://www.greenfield.com/RegistrationGateway/Messages"">
<Registration xmlns=""http://www.greenfield.com/RegistrationGateway/Types"">
<Source>
<SourceID>" + sourceID + #"</SourceID>
</Source>
<Email>" + email + #"</Email>
<FirstName>" + firstName + #"</FirstName>
<LastName>" + lastName + #"</LastName>
<CountryUK>
<CountryID>" + countryID + #"</CountryID>
<Language>" + countryLanguage + #"</Language>
<Address>
<Postalcode>" + postalCode + #"</Postalcode>
</Address>
</CountryUK>
<DOB>" + dob + #"</DOB>
<Gender>" + gender + #"</Gender>
</Registration>
</SubmitPanelist>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://coreg.surveycenter.com/RegistrationGateway/PanelistService.asmx");
//if (proxy != null) req.Proxy = new WebProxy(proxy, true);
// req.Headers.Add("GetClientInfo", "http://tempuri.org/GetClientInfo");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
Response.Write(r.ReadToEnd());
//Response.Write(stm.ToString());
//Response.Write(r.ToString());
Response.End();
}
}
}
Update
As suggested by Darin. I did as instructed however the following line of code
using (var client = new RegistrationBindingsClient("RegistrationBindings"))
gives the error
The type or namespace name 'RegistrationBindingsClient' could not be found (are you missing a using directive or an assembly reference?)
Any help will be greatly appreciated
The web service you are trying to consume offers a WSDL at the following address. So simply right click on the References in the solution explorer and use the Add Service Reference dialog in Visual Studio and point to the WSDL and it will generate strongly typed classes for you to easily consume the service, just like this:
protected void sendSoapMessage()
{
using (var client = new RegistrationBindingsClient("RegistrationBindings"))
{
var registration = new RegistrationType();
registration.Source = new SourceType();
registration.Source.SourceID = "50001255";
registration.Email = "adsvine#gmail.com";
registration.FirstName = "Muz";
registration.LastName = "Khan";
var countryUK = new CountryTypeUK();
countryUK.CountryID = 2000077;
countryUK.Language = 2000240;
countryUK.Address = new AddressTypeUK();
countryUK.Address.Postalcode = "N19 3NU";
registration.Item = countryUK;
registration.DOB = new DateTime(1977, 3, 8);
registration.Gender = 2000247;
client.SubmitPanelist(registration);
}
}
See how easy it is. You should not worry about any SOAP and XML plumbing.
And if you are interested in the actual underlying SOAP envelope that is being sent on the wire using this request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SubmitPanelist xmlns="http://www.greenfield.com/RegistrationGateway/Messages">
<Registration xmlns="http://www.greenfield.com/RegistrationGateway/Types">
<Source>
<SourceID>50001255</SourceID>
</Source>
<Email>adsvine#gmail.com</Email>
<FirstName>Muz</FirstName>
<LastName>Khan</LastName>
<CountryUK>
<CountryID>2000077</CountryID>
<Language>2000240</Language>
<Income>0</Income>
<Education>0</Education>
<Address>
<Postalcode>N19 3NU</Postalcode>
</Address>
</CountryUK>
<DOB>1977-03-08</DOB>
<Gender>2000247</Gender>
</Registration>
</SubmitPanelist>
</s:Body>
</s:Envelope>
Is there any error message or did you use an HTTP monitor?
Some maybe useful Links:
XmlDocument Save Method
How to: Send Data Using the WebRequest Class
Similar Question in stackoverflow maybe helpful
You can access the service in 2 methods:
By adding a web reference of the service. In visual studio you can right click on your project and select the option Add Web reference and then paste the URL of the service.
Generate client proxy from the wsdl using wsdl tool from Visual studio command prompt.The command would be as follows:
c:>wsdl "http://coreg.surveycenter.com/RegistrationGateway/PanelistService.asmx?wsdl
It would generate a .cs file and an output.config. Include the .cs file in your project and you can directly use it to access the service. Make sure that the config file entries are added to your projects config.
If you want to use HttpWebRequest then find the code below :
string soap =
#"<?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>
<Register xmlns=""http://tempuri.org/"">
<id>123</id>
<data1>string</data1>
</Register>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/WebServices/CustomerWebService.asmx");
req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
// TODO: Do whatever you need with the response
My Service looks like :
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CustomerWebService : System.Web.Services.WebService
{
[WebMethod]
public string Register(long id, string data1)
{
return "ID.CUSTOMER";
}
}
Here's a general purpose class you can use to achieve what you need.
IMPORTANT DISCLAIMER: You should only use this when you want (or need) to manually issue a SOAP-based web service: in most common scenarios you should definitely use the Web Service WSDL together with the Add Service Reference Visual Studio feature, which is the proper way to do that.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml;
namespace Ryadel.Web.SOAP
{
/// <summary>
/// Helper class to send custom SOAP requests.
/// </summary>
public static class SOAPHelper
{
/// <summary>
/// Sends a custom sync SOAP request to given URL and receive a request
/// </summary>
/// <param name="url">The WebService endpoint URL</param>
/// <param name="action">The WebService action name</param>
/// <param name="parameters">A dictionary containing the parameters in a key-value fashion</param>
/// <param name="soapAction">The SOAPAction value, as specified in the Web Service's WSDL (or NULL to use the url parameter)</param>
/// <param name="useSOAP12">Set this to TRUE to use the SOAP v1.2 protocol, FALSE to use the SOAP v1.1 (default)</param>
/// <returns>A string containing the raw Web Service response</returns>
public static string SendSOAPRequest(string url, string action, Dictionary<string, string> parameters, string soapAction = null, bool useSOAP12 = false)
{
// Create the SOAP envelope
XmlDocument soapEnvelopeXml = new XmlDocument();
var xmlStr = (useSOAP12)
? #"<?xml version=""1.0"" encoding=""utf-8""?>
<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>
<{0} xmlns=""{1}"">{2}</{0}>
</soap12:Body>
</soap12:Envelope>"
: #"<?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>
<{0} xmlns=""{1}"">{2}</{0}>
</soap:Body>
</soap:Envelope>";
string parms = string.Join(string.Empty, parameters.Select(kv => String.Format("<{0}>{1}</{0}>", kv.Key, kv.Value)).ToArray());
var s = String.Format(xmlStr, action, new Uri(url).GetLeftPart(UriPartial.Authority) + "/", parms);
soapEnvelopeXml.LoadXml(s);
// Create the web request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", soapAction ?? url);
webRequest.ContentType = (useSOAP12) ? "application/soap+xml;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\"";
webRequest.Accept = (useSOAP12) ? "application/soap+xml" : "text/xml";
webRequest.Method = "POST";
// Insert SOAP envelope
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
// Send request and retrieve result
string result;
using (WebResponse response = webRequest.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
result = rd.ReadToEnd();
}
}
return result;
}
}
}
For additional info & details regarding this class you can also read this post on my blog.