WebService calling through HTTP object programming in C #

Source: Internet
Author: User
Tags urlencode

1. WebService help class

Bytes ---------------------------------------------------------------------------------------------------------------

Public class webservicehelper
{
Public static string callservicebyget (string strurl)
{
// String strurl = "http: // localhost: 12074/service1.asmx/getproductprice? Productid = ";
// Strurl + = This. textbox1.text;
// Create an HTTP request
Httpwebrequest request = (httpwebrequest) webrequest. Create (strurl );
// Request. method = "get ";
Httpwebresponse response = (system. net. httpwebresponse) request. getresponse ();
Stream S = response. getresponsestream ();
// Convert to XML for processing
Xmltextreader reader = new xmltextreader (s );
Reader. movetocontent ();
String strvalue = reader. readinnerxml ();
Reader. Close ();
Strvalue = strvalue. Replace ("<", "<");
Strvalue = strvalue. Replace (">", "> ");
Return strvalue;
}
Public static string callservicebypost (string strurl, system. Collections. Specialized. stringdictionary parameters)
{
// String strurl = "http: // localhost: 12074/service1.asmx/getproductprice ";
// Create an HTTP request
Httpwebrequest request = (httpwebrequest) webrequest. Create (strurl );
// POST Request Method
Request. method = "Post ";
// Content type
Request. contenttype = "application/X-WWW-form-urlencoded ";
// Set parameters and perform URL Encoding
Stringbuilder codedstring = new stringbuilder ();
Foreach (string key in parameters. Keys)
{
Codedstring. append (httputility. urlencode (key ));
Codedstring. append ("= ");
Codedstring. append (httputility. urlencode (parameters [Key]);
Codedstring. append ("&");
}
String paraurlcoded = codedstring. Length = 0? String. Empty: codedstring. tostring (). substring (0, codedstring. Length-1 );
// String paraurlcoded = httputility. urlencode ("productid ");
// Paraurlcoded + = "=" + httputility. urlencode (this. textbox1.text );
Byte [] payload;
// Convert the URL-encoded string into bytes
Payload = system. Text. encoding. utf8.getbytes (paraurlcoded );
// Set the request contentlength
Request. contentlength = payload. length;
// Send a request to obtain the request stream
Stream writer = request. 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) request. getresponse ();
Stream S = response. getresponsestream ();
// Convert to XML for processing
Xmltextreader reader = new xmltextreader (s );
Reader. movetocontent ();
String strvalue = reader. readinnerxml ();
Reader. Close ();
Strvalue = strvalue. Replace ("<", "<");
Strvalue = strvalue. Replace (">", "> ");
Return strvalue;
}
}

Bytes ---------------------------------------------------------------------------------------------------------------

2. WebService Method

Bytes ---------------------------------------------------------------------------------------------------------------

/// <Summary>
/// Summary of service1
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Toolboxitem (false)]
Public class service1: system. Web. Services. WebService
{

[Webmethod]
Public String helloworld1 ()
{
Return "Hello World ";
}

[Webmethod]
Public String helloworld2 (string P1, string P2)
{
Return string. Concat ("Hello World", P1, ",", P2 );
}
[Webmethod]
Public String helloworld3 (string datetime)
{
Return string. Concat ("Hello World", "Today is", datetime );
}
}

Bytes ---------------------------------------------------------------------------------------------------------------

3. Use the WebService help class to call the WebService Method

Bytes ---------------------------------------------------------------------------------------------------------------

// Call with two string Parameters
String url = "http: // localhost/bydif. WebService/service. asmx/helloworld2 ";
Stringdictionary parameters = new stringdictionary ();
Parameters. Add ("p1", "123 ");
Parameters. Add ("p2", "456 ");
String message = webservicehelper. callservicebypost (URL, parameters );
System. Console. writeline (Message );
// Call without Parameters
Url = "http: // localhost/bydif. WebService/service. asmx/helloworld1 ";
Parameters = new stringdictionary ();
Message = webservicehelper. callservicebypost (URL, parameters );
System. Console. writeline (Message );
// Call with a date string
Url = "http: // localhost/bydif. WebService/service. asmx/helloworld3 ";
Parameters = new stringdictionary ();
Parameters. Add ("datetime", system. datetime. Now. tow.datestring ());
Message = webservicehelper. callservicebypost (URL, parameters );
System. Console. writeline (Message );
System. Console. Read ();

Bytes ---------------------------------------------------------------------------------------------------------------

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.