No return value. WebService is called in soap mode.

Source: Internet
Author: User

Using System;
Using System. Web;
Using System. Xml;
Using System. Collections;
Using System. Net;
Using System. Text;
Using System. IO;
Using System. Xml. Serialization;

Namespace XTask. Common. Net
{
/// <Summary>
/// Use WebRequest/WebResponse to call WebService
/// </Summary>
Public class WebSvcCaller
{
// Add the following content to the configuration file
// <WebServices>
// <Protocols>
// <Add name = "HttpGet"/>
// <Add name = "HttpPost"/>
/// </Protocols>
// </WebServices>

Private static Hashtable _ xmlNamespaces = new Hashtable (); // cache xmlNamespace to avoid repeated calls to GetNamespace

/// <Summary>
/// Call webService
/// </Summary>
/// <Param name = "URL"> Format: http: // localhost/emailws/emailService. asmx/HelloWorld </param>
Public static void QuerySoapWebServiceNoReturn (String URL)
{
String MethodName = URL. Substring (URL. LastIndexOf ("/") + 1 );
String realUrl = URL. Substring (0, URL. LastIndexOf ("/"));

If (_ xmlNamespaces. ContainsKey (URL ))
{
QuerySoapWebService (realUrl, MethodName, null, _ xmlNamespaces [realUrl]. ToString ());
}
Else
{
QuerySoapWebService (realUrl, MethodName, null, GetNamespace (realUrl ));
}
}

/// <Summary>
/// Method with returned values.
/// </Summary>
/// <Param name = "URL"> </param>
/// <Param name = "MethodName"> </param>
/// <Param name = "Pars"> </param>
/// <Param name = "XmlNs"> </param>
/// <Returns> </returns>
Private static void QuerySoapWebService (String URL, String MethodName, Hashtable Pars, string XmlNs)
{
_ XmlNamespaces [URL] = XmlNs; // Add cache to improve efficiency
HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (URL );
Request. Method = "POST ";
Request. ContentType = "text/xml; charsets = UTF-8 ";
Request. Headers. Add ("SOAPAction", "\" "+ XmlNs + (XmlNs. EndsWith ("/")? "": "/") + MethodName + "\"");
SetWebRequest (request );
Byte [] data = EncodeParsToSoap (Pars, XmlNs, MethodName );

WriteRequestData (request, data );
Request. GetResponse ();
}

/// <Summary>
/// Obtain the namespace
/// </Summary>
/// <Param name = "URL"> </param>
/// <Returns> </returns>
Private static string GetNamespace (String URL)
{
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (URL + "? WSDL ");
SetWebRequest (request );
WebResponse response = request. GetResponse ();
StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding. UTF8 );
XmlDocument doc = new XmlDocument ();
Doc. LoadXml (sr. ReadToEnd ());
Sr. Close ();
Return doc. SelectSingleNode ("// @ targetNamespace"). Value;
}

/// <Summary>
/// Convert to a soap package
/// </Summary>
/// <Param name = "Pars"> parameter </param>
/// <Param name = "XmlNs"> namespace </param>
/// <Param name = "MethodName"> method name </param>
/// <Returns> return value </returns>
Private static byte [] EncodeParsToSoap (Hashtable Pars, String XmlNs, String MethodName)
{
XmlDocument doc = new XmlDocument ();
Doc. loadXml ("<soap: Envelope xmlns: xsi = \" external "xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap = \" external "> </soap: envelope> ");
AddDelaration (doc );
XmlElement soapBody = doc. CreateElement ("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope ");
XmlElement soapMethod = doc. CreateElement (MethodName );
SoapMethod. SetAttribute ("xmlns", XmlNs );
If (Pars! = Null & Pars. Count> 0)
{
Foreach (string k in Pars. Keys)
{
XmlElement soapPar = doc. CreateElement (k );
SoapPar. InnerXml = ObjectToSoapXml (Pars [k]);
SoapMethod. AppendChild (soapPar );
}
}
SoapBody. AppendChild (soapMethod );
Doc. DocumentElement. AppendChild (soapBody );
Return Encoding. UTF8.GetBytes (doc. OuterXml );
}

/// <Summary>
/// Convert the object to xml
/// </Summary>
/// <Param name = "o"> </param>
/// <Returns> </returns>
Private static string ObjectToSoapXml (object o)
{
XmlSerializer mySerializer = new XmlSerializer (o. GetType ());
MemoryStream MS = new MemoryStream ();
MySerializer. Serialize (MS, o );
XmlDocument doc = new XmlDocument ();
Doc. LoadXml (Encoding. UTF8.GetString (ms. ToArray ()));
If (doc. DocumentElement! = Null)
{
Return doc. DocumentElement. InnerXml;
}
Else
{
Return o. ToString ();
}
}

/// <Summary>
/// Set Request Parameters
/// </Summary>
/// <Param name = "request"> </param>
Private static void SetWebRequest (HttpWebRequest request)
{
Request. Credentials = CredentialCache. DefaultCredentials;
Request. Timeout = 10000;
}

/// <Summary>
/// Write data to the network stream
/// </Summary>
/// <Param name = "request"> </param>
/// <Param name = "data"> </param>
Private static void WriteRequestData (HttpWebRequest request, byte [] data)
{
Request. ContentLength = data. Length;
Stream writer = request. GetRequestStream ();
Writer. Write (data, 0, data. Length );
Writer. Close ();
}

/// <Summary>
/// Add head in xml
/// </Summary>
/// <Param name = "doc"> </param>
Private static void AddDelaration (XmlDocument doc)
{
XmlDeclaration decl = doc. CreateXmlDeclaration ("1.0", "UTF-8", null );
Doc. InsertBefore (decl, doc. DocumentElement );
}

}
}

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.