Comparison of three methods to access ASP. NET Web Services

Source: Internet
Author: User
Access using HTTP-GET
HTTP-GET access # region HTTP-GET access
Private void button#click (Object sender, system. eventargs E)
{
System. net. httpwebrequest req;
String strget = "http: // localhost/helloword? Name = ";
Strget = strget + textbox1.text;
Rep = (system.net. httpwebrequest) webrequest. Create (strget );
System. net. httpwebresponse response;
Response = (system.net. httpwebresponse). Req. getresponse ();
System. Io. Stream S;
S = response. getresponsestream ();
Xmltextreader reader = new xmltextreader (s );
Reader. movetocontent ();
Label1.text = reader. readerinnerxml ();
Reader. Close ();
}
# Endregion

Access using HTTP-POST

HTTP-POST access # region HTTP-POST access
Private void button#click (Object sender, system. eventargs E)
{
String URL;
Url = "http: // localhost/helloword ";
Httpwebrequest req;
Req = (httpwebrequest) webrequest. Create (URL );

// POST request
Rep. method = "Post ";

// Content type
Rep. contenttype = "application/X-WWW-form-urlencoded ";

// The parameter is URL encoded
String paraurlcoded = system. Web. httputility. urlencode ("name ");
Paraurlcoded = paraurlcoded + "=" + system. Web. httputility. urlencode (textbox1.text );
Byte [] payload;

// Convert the URL-encoded string into bytes
Payload = system. textencoding. utf8.getbytes (paraurlcoded );

// Set the request contentlength
Req. contentlength = payload. length;

// Obtain the request stream
Stream writer = Req. getrequeststream ();

// Write request parameters to the stream
Writer. Write (payload, 0, payload. Length );

// Close the request stream
Writer. Close ();

// Obtain the response stream
Httpwebresponse response = (httpwebresponse) Req. getresponse ();
System. Io. Stream S;
S = response. getresponsestream ();
Xmltextreader reader = new xmltextreader (s );
Reader. movetocontent ();
Label1.text = reader. readerinnerxml ();
Reader. Close ();
}
# Endregion

Access through soap

First, the customer agent uses the xmlserializer object to serialize parameters to XML.
Xmlserializer object deserialization to obtain the call parameters from XML.
The person class is as follows:

Person class # region person class
Public class person
{
Private string name;
Private int age;
Public string name;
{
Get
{
Return name;
}
Set
{
Name = value;
}
}

Public int age
{
Get
{
Return age;
}
Set
{
Age = value;
}
}

Public void show ()
{

Console. writeline ("name" = {0}, age = {1}, name, age );

}
}

# Endregionsoap access # region soap access
Static void main (string [] ARGs)
{
// Instantiate the XML sequencer
Xmlserializer serializer = net xmlserializer (typeof (person ));
// Generate a serialized object
Person P = new person ();
P. Age = 22;
P. Name = "Jack. Xiao ";
// Generate a memory stream
Memorystream S = new memorystream ();
// Writer
Streamwriter writer = new streamwriter (S, encoding. utf8 );
Serializer. serialize (writer, P );
// Locate the stream Header
S. Seek (0, seekorigin. Begin );
Streamreader reader = new streamreader (S, encoding. utf8 );

Person P2 = (person) serializer. deserialize (Reader );
P2.show ();
Console. writeline ("=========== XML content ");
// Display XML content
S. Seek (0, seekorigin. Begin );
String xmls = reader. Readline ()
While (xmls! = NULL)
{
Console. writeline (xmls );
Xmls = reader. Readline ();
}
// Close the stream
Reader. Close ();
}
# Endregion

Related Article

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.