Call the WebService deployed under SSL

Source: Internet
Author: User
Call the WebService method deployed under SSL (Microsoft. NET Framework SDK V2.0)

Private void btncall_click (Object sender, eventargs E)
{
// SOAP request information (obtained from the WSDL Analysis)
Stringbuilder sb = new stringbuilder ();
SB. append ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ R \ n ");
SB. append ("<soap: envelope xmlns: xsi = \" external "xmlns: XSD = \" http://www.w3.org/2001/xmlschema\ "xmlns: Soap = \" external "> \ r \ n ");
SB. append ("<soap: Body> \ r \ n ");
SB. append ("<m: getdata xmlns = \" http: // 127.0.0.1/xfirewebservice/services/helloworld \ "/> \ r \ n ");
// Sb. append ("<getdata xmlns = \" http://tempuri.org/\ "/> \ r \ n ");
SB. append ("</soap: Body> \ r \ n ");
SB. append ("</soap: envelope> \ r \ n ");
Byte [] sendbyte = system. Text. encoding. utf8.getbytes (sb. tostring ());
 
Try
{
// Load the certificate (this certificate is exported from IE)
String certificatefile = @ "C: \ out. Cer ";
X509certificate certificate = x509certificate. createfromcertfile (certificatefile );
 
// Verify
Servicepointmanager. servercertificatevalidationcallback = new system. net. Security. remotecertificatevalidationcallback (checkvalidationresult );
 
// Generate a Web Request
Httpwebrequest request = (httpwebrequest) webrequest. Create ("https: // 192.168.100.153/cupit/messageio ");
// Httpwebrequest request = (httpwebrequest) webrequest. Create ("http: // localhost: 2022/WS/service. asmx ");

// Set Request attributes
Request. clientcertificates. Add (certificate );
Request. method = "Post ";
Request. contenttype = "text/XML; charsets = UTF-8 ";
Request. contentlength = sendbyte. length;
 
Stream rstream = request. getrequeststream ();
// Send the soap File
Rstream. Write (sendbyte, 0, sendbyte. Length );
 
// Obtain the Server Response (return value)
Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream receivestream = response. getresponsestream ();
Streamreader readstream = new streamreader (receivestream, encoding. utf8 );
 
// Save the reverse return value to the file
String getdatafile = @ "C: \ getdata. xml ";
If (file. exists (getdatafile ))
{
File. Delete (getdatafile );
}
Filestream FS = new filestream (getdatafile, filemode. Create );
Streamwriter Sw = new streamwriter (FS, encoding. utf8 );
 
Sw. Write (readstream. readtoend ());
Sw. Close ();
FS. Close ();
 

MessageBox. Show ("OK ");
Request. getresponse ();
Response. Close ();
Readstream. Close ();
}
Catch (exception ex)
{
 
MessageBox. Show (ex. tostring ());
}
}
Public bool checkvalidationresult (Object sender, x509certificate certificate, x509chain chain, sslpolicyerrors errors)
{
Return true;
}

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.