C # uses SOAP to invoke Web Service

Source: Internet
Author: User

program code using System;
Using System.IO;
Using System.Net;
Using System.Text;

Namespace ConsoleApplication1
{

Class Program
{

static void Main (string[] args)
{
Constructing SOAP Request Information
StringBuilder soap = new StringBuilder ();
Soap. Append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");
Soap. Append ("<soap:envelope xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:xsd=\" http://www.w3.org /2001/xmlschema\ "xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ ">");
Soap. Append ("<soap:Body>");
Soap. Append ("<getweather xmlns=\" http://webxml.com.cn/\ ">");
Soap. Append ("<theCityCode>2210</theCityCode>");
Soap. Append ("<theUserID></theUserID>");
Soap. Append ("</getWeather>");
Soap. Append ("</soap:Body>");
Soap. Append ("</soap:Envelope>");

Initiating a request
Uri uri = new Uri ("Http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
WebRequest WebRequest = WebRequest.Create (URI);
Webrequest.contenttype = "Text/xml; Charset=utf-8 ";
Webrequest.method = "POST";
using (Stream Requeststream = Webrequest.getrequeststream ())
{
byte[] parambytes = Encoding.UTF8.GetBytes (soap. ToString ());
requestStream.Write (parambytes, 0, parambytes.length);
}

Response
WebResponse WebResponse = Webrequest.getresponse ();
using (StreamReader Mystreamreader = new StreamReader (WebResponse.GetResponseStream (), Encoding.UTF8))
{
Console.WriteLine (Mystreamreader.readtoend ());
}

Console.readkey ();
}
}

}


return Result:

Reference content <?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><getweatherresponse xmlns= "http://WebXml.com.cn/" ><getweatherresult><string > Fujian Fuzhou </string><string> Fuzhou </string><string>2210</string><string>2010/01/ 15:42:15</string><string> Weather Today: Temperature: 12.5 ℃, Wind/Wind: Southeast Wind 4, Humidity: 94%, air pressure:1011.9hpa</string>< string> air quality: good; UV intensity: no </string><string> clothing index: Recommended for thin-type suits or denim shirt pants, such as spring and autumn transition clothing. The frail and infirm are suitable for suits, jackets and so on.
Cold index: There will be a strong cooling process, and the air humidity is large, very prone to colds, please pay special attention to increase clothing warm cold.
Morning Exercise index: There are showers, less suitable for morning exercises, if you insist on outdoor exercise, please bring rain gear. It is advisable to reduce morning exercise time for frail people.
Car Wash Index: not suitable to wash the car, the next 24 hours of rain, if during this period of washing, rain and muddy the road may be dirty your car again.
Drying index: Occasional rainfall may be wet drying clothes, not suitable for drying. Please keep an eye out for weather changes.
Tourism index: There is a shower, the temperature is appropriate, in the rain play do not have a mood, you can not miss the opportunity yo! But remember to carry rain gear.
Traffic index: Light rain, wet road, easy to skid vehicles, please drive carefully.
Comfort Index: The daytime is not too hot and not too cold, the wind is not big, I believe you in such weather conditions, you should feel relatively fresh and comfortable. </string><string>1 month 21st showers turn to drizzle </string><string>12℃/19℃</string><string> No lasting wind breeze </string><string>3.gif</string><string>7.gif</string><string>1 month 22nd Light rain </string><string>9℃/14℃</string><string> no continuous wind breeze </string><string>7.gif </string><string>7.gif</string><string>1 Month 23rd light Rain </string><string>8℃/12℃</ String><string> no continuous wind breeze </string><string>7.gif</string><string>7.gif</string ><string>1 Month 24th Yin </string><string>12℃/17℃</string><string> no continuous wind breeze </string ><string>2.gif</string><string>2.gif</string><string>1 Month 25th Cloudy </string> <string>13℃/18℃</string><string> no continuous wind breeze </string><string>1.gif</string>< String>1.gif</string></getweatherresult></getweatherresponse>&lT;/soap:body></soap:envelope>


ASP Edition

Copy content to Clipboard program code <%
Dim Xmlhttp,soap

SOAP = Soap & "<?xml version=" "1.0" "encoding=" "Utf-8" "?>"
SOAP = Soap & "<soap:envelope xmlns:soap=" "http://schemas.xmlsoap.org/soap/envelope/" "xmlns:xsd=" "/HTTP/ Www.w3.org/2001/XMLSchema "" xmlns:xsi= "" Http://www.w3.org/2001/XMLSchema-instance "" > "
SOAP = Soap & "<soap:Body>"
SOAP = Soap & "<getweather xmlns=" "http://WebXml.com.cn/" ">"
SOAP = Soap & "<theCityCode>2210</theCityCode>"
SOAP = Soap & "<theUserID></theUserID>"
SOAP = Soap & "</getWeather>"
SOAP = Soap & "</soap:Body>"
SOAP = Soap & "</soap:Envelope>"

Set xmlHttp = CreateObject ("MSXML2. XMLHTTP ")
Xmlhttp.open "Post", "Http://www.webxml.com.cn/WebServices/WeatherWS.asmx", False
Xmlhttp.setrequestheader "Content-type", "text/xml; Charset=utf-8 "
' Have seen the WS requirements of Java must have SOAPAction, otherwise error no soapaction header!
Xmlhttp.setrequestheader "SOAPAction", "Http://WebXml.com.cn/getWeather"
Xmlhttp.send (SOAP)
If Xmlhttp.status =
Response.Write (XmlHttp.responseXML.xml)
End If
Set xmlHttp = Nothing
%>


Web service calls with SoapHeader

Copy content to Clipboard program code using System;
Using System.IO;
Using System.Net;
Using System.Text;

Namespace ConsoleApplication1
{

Class Program
{

static void Main (string[] args)
{
Constructing SOAP Request Information
StringBuilder soap = new StringBuilder ();
Soap. Append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");
Soap. Append ("<soap:envelope xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:xsd=\" http://www.w3.org /2001/xmlschema\ "xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ ">");
Soap. Append ("<soap:Header>");
Soap. Append ("<mysoapheader xmlns=\" http://www.mzwu.com/\ "soap:mustunderstand=\" 1\ ">");//mustunderstand=1, The recipient must be able to handle this soapheader information or return an error
Soap. Append ("<UserName>admin</UserName>");
Soap. Append ("<UserPass>admin888</UserPass>");
Soap. Append ("</MySoapHeader>");
Soap. Append ("</soap:Header>");
Soap. Append ("<soap:Body>");
Soap. Append ("Soap. Append ("<name>dnawo</name>");
Soap. Append ("</Hello>");
Soap. Append ("</soap:Body>");
Soap. Append ("</soap:Envelope>");

Initiating a request
Uri uri = new Uri ("Http://localhost:39674/Service1.asmx");
WebRequest WebRequest = WebRequest.Create (URI);
Webrequest.contenttype = "Text/xml; Charset=utf-8 ";
Webrequest.method = "POST";
using (Stream Requeststream = Webrequest.getrequeststream ())
{
byte[] parambytes = Encoding.UTF8.GetBytes (soap. ToString ());
requestStream.Write (parambytes, 0, parambytes.length);
}

Response
WebResponse WebResponse = Webrequest.getresponse ();
using (StreamReader Mystreamreader = new StreamReader (WebResponse.GetResponseStream (), Encoding.UTF8))
{
Console.WriteLine (Mystreamreader.readtoend ());
}

Console.readkey ();
}
}

}


Little Tricks

When viewing a Web service in a browser, there are detailed examples of SOAP requests and responses:


Resources

· Soap Tutorial: http://www.w3school.com.cn/soap/index.asp
• Simple Object Access Protocol: Http://zh.wikipedia.org/zh-cn/SOAP
· Soap Call Web Service:http://book.51cto.com/art/200906/129733.htm
• About lang Xin webserver interface problem: http://fpcfjf.blog.163.com/blog/static/5546979320081069223827/

C # uses SOAP to invoke Web Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.