C#webservice client requests through HTTP calls

Source: Internet
Author: User
Tags urlencode

1.webservice Helper Classes

public class Webservicehelper
{
public static string Callservicebyget (String strurl)
{

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 and process yourself
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)
{
Create an HTTP request
HttpWebRequest request = (HttpWebRequest) webrequest.create (strURL);
Post request mode
Request. Method = "POST";
Content Type
Request. ContentType = "application/x-www-form-urlencoded";
Setting parameters and 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;
Converts a URL-encoded string into bytes
Payload = System.Text.Encoding.UTF8.GetBytes (paraurlcoded);
Set the contentlength of the request
Request. ContentLength = payload. Length;
Send request, GET request stream
Stream writer = Request. GetRequestStream ();
Write request parameters to stream
Writer. Write (payload, 0, payload. Length);
Close the request flow
Writer. Close ();
Get the response stream
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
Stream s = response. GetResponseStream ();
Convert to XML and process yourself
XmlTextReader Reader = new XmlTextReader (s);
Reader.movetocontent ();
string strvalue = Reader.readinnerxml ();
Reader.close ();
strvalue = Strvalue.replace ("<", "<");
strvalue = Strvalue.replace (">", ">");
return strvalue;
}
}

---------------------------------------------------------------------------------------------------------------

2.webservice method

---------------------------------------------------------------------------------------------------------------

<summary>
Summary description of Service1
</summary>
[WebService (Namespace = "HTTP://127.0.0.1/")]
[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);
}
}

---------------------------------------------------------------------------------------------------------------

3. Use the WebService helper class to invoke the WebService method

---------------------------------------------------------------------------------------------------------------

Calls with 2 string arguments
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);
Calls with no arguments
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.ToShortDateString ());
message = Webservicehelper.callservicebypost (URL, parameters);
System.Console.WriteLine (message);
System.Console.Read ();

C#webservice client requests via HTTP call (GO)

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.