[Reprint] Use httpwebrequest to call WebService in the background

Source: Internet
Author: User

 

Directory:
1. Business Requirements for WebService calling in the background
2 interaction protocols supported by WebService
3
How to configure the protocols supported by WebService
4. WebService call in the background
4.1 soap 1.1 Background call instance
4.2
Soap 1.2 Background call instance
Note: the development environment of this article is vss2008. NET Framework.
3.5
This article designs the WebService for the sample code.
Is
Service path: http: // localhost/webservicetest/service1.asmx
Service Interface:
[Webmethod]
  
Public String helloworld (string studentname, string password)
{
  
Return "Hello World ";
}
1. Business Requirements for WebService calling in the background
  
In the actual development environment, when we call WebService, the client proxy class is generated by referencing the asmx file of the actually deployed WebService in the project. In this way, it is encapsulated with WebService and called as a proxy class, which is easy to use and quick to develop.
This development method involves two important issues.
1)
In the development environment, you must be able to access the WebService to be called. When developing Intranet systems of some large companies, we often cannot access them in the development environment, but only in the deployment environment.
2) When the WebService Interface version changes, our application system needs to be re-compiled and deployed.
  

After discovering the above confusions, we intuitively tell us that we need to access WebService directly through interactive protocols. Like web crawlers, they interact with business operations.
  
2
Interaction protocols supported by WebService
  
WebService supports three methods
1) HTTP POST
(Note that this method is only used for local debugging. If the Web Service is deployed on another machine, the application cannot be called through http post)
  
The interaction format is as follows:
Post/webservicetest/service1.asmx/helloworld HTTP/1.1
Host:
Localhost
Content-Type:
Application/X-WWW-form-urlencoded
Content-Length:
Length
Studentname = string & Password = string
2) soap1.1 Protocol
Note that the SOAP protocol is based on HTTP, that is, encapsulation Based on HTTP
The interaction format is as follows:
Post
/Webservicetest/service1.asmx HTTP/1.1
HOST: localhost
Content-Type:
Text/XML; charset = UTF-8
Content-Length: Length
Soapaction:
Http://tempuri.org/HelloWorld"
<? 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>
<Helloworld xmlns = "http://tempuri.org/">
  
<Studentname> string </studentname>
  
<Password> string </password>
</Helloworld>
  
</Soap: Body>
</Soap: envelope>
3) soap1.2
Protocol
The interaction format is as follows:
Post/webservicetest/service1.asmx HTTP/1.1
Host:
Localhost
Content-Type: Application/soap + XML;
Charset = UTF-8
Content-Length: Length
<? 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>
<Helloworld xmlns = "http://tempuri.org/">
  
<Studentname> string </studentname>
  
<Password> string </password>
</Helloworld>
  
</Soap12: Body>
</Soap12: envelope>
3
How to configure the protocols supported by WebService
WebService supports two Protocols: soap1.1 soap1.2
You can configure the configuration file to support those protocols. By default
Both protocols are supported.
The specific configuration method is as follows:
In the configuration file
<WebServices>
  
<Protocols>
<Add name = "httpsoap1.2"/>
<Add
Name = "httpsoap1.1"/>
</Protocols>
</WebServices>
  
4
WebService call in the background
4.1 soap 1.1 Background call instance
String str1 = "/" Double quotation marks /"";
  
Console. writeline ("new connection test ");
String Param = @ "<? 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>
<Helloworld xmlns = "" http://tempuri.org/"">
  
<Studentname> 1 </studentname>
  
<Password> 1 </password>
</Helloworld>
  
</Soap: Body>
</Soap: envelope> ";
Byte [] BS =
Encoding. utf8.getbytes (PARAM );
Httpwebrequest myrequest =
(Httpwebrequest) webrequest. Create ("http: // fox-gaolijun/short_message/service1.asmx ");
  
Myrequest. method = "Post ";
Myrequest. contenttype = "text/XML;
Charset = UTF-8 ";
Myrequest. headers. Add ("soapaction ",
Http://tempuri.org/HelloWorld ");
Myrequest. contentlength =
BS. length;
Console. writeline ("complete preparation ");
Using (Stream reqstream =
Myrequest. getrequeststream ())
{
Reqstream. Write (BS, 0,
BS. Length );
}
  
Using (httpwebresponse myresponse =
(Httpwebresponse) myrequest. getresponse ())
{
Streamreader sr = new
Streamreader (myresponse. getresponsestream (), encoding. utf8 );
  
Responsestring = Sr. readtoend ();
Console. writeline ("feedback result" +
Responsestring );
}
Console. writeline ("call completed interface ");
}
Catch
(Exception E)
{
  
Console. writeline (system. datetime. Now. tow.timestring () + "lbs exception:" +
E. Message );
Console. writeline (E. stacktrace );
}
4.1 soap 1.2
Backend call instance
Console. writeline ("new connection test ");
String responsestring;
  
String Param = @ "<? 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>
<Helloworld xmlns = "" http://tempuri.org/"">
  
<Studentname> 1212 </studentname>
  
& Lt; password & gt; 12121 & lt;/password & gt;
</Helloworld>
  
</Soap12: Body>
</Soap12: envelope> ";
Byte [] BS =
Encoding. utf8.getbytes (PARAM );
Httpwebrequest myrequest =
(Httpwebrequest) webrequest. Create ("
Http: // fox-gaolijun/short_message/service1.asmx ");
Myrequest. method =
"Post ";
Myrequest. contenttype = "application/soap + XML;
Charset = UTF-8 ";
Myrequest. contentlength = BS. length;
  
  
Console. writeline ("complete preparation ");
Using (Stream reqstream =
Myrequest. getrequeststream ())
{
Reqstream. Write (BS, 0,
BS. Length );
}
Using (httpwebresponse myresponse =
(Httpwebresponse) myrequest. getresponse ())
{
Streamreader sr = new
Streamreader (myresponse. getresponsestream (), encoding. utf8 );
  
Responsestring = Sr. readtoend ();
Console. writeline ("feedback result" +
Responsestring );
}
Console. writeline ("call completed interface ");
}
Catch
(Exception E)
{
  
Console. writeline (system. datetime. Now. tow.timestring () + "lbs exception:" +
E. Message );
Console. writeline (E. stacktrace );
}

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.