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)