標籤:
1.webservice協助類
public class WebServiceHelper
{
public static string CallServiceByGet(string strURL)
{
//建立一個HTTP請求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="get";
HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
//轉化為XML,自己進行處理
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)
{
//建立一個HTTP請求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post請求方式
request.Method = "POST";
//內容類型
request.ContentType = "application/x-www-form-urlencoded";
//設定參數,並進行URL編碼
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;
//將URL編碼後的字串轉化為位元組
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//佈建要求的ContentLength
request.ContentLength = payload.Length;
//發送請求,獲得請求流
Stream writer = request.GetRequestStream();
//將請求參數寫入流
writer.Write(payload, 0, payload.Length);
//關閉請求流
writer.Close();
//獲得響應流
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
//轉化為XML,自己進行處理
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
Reader.Close();
strValue = strValue.Replace("<", "<");
strValue = strValue.Replace(">", ">");
return strValue;
}
}
---------------------------------------------------------------------------------------------------------------
2.webservice方法
---------------------------------------------------------------------------------------------------------------
/// <summary>
/// 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.使用webservice協助類調用webservice方法
---------------------------------------------------------------------------------------------------------------
//帶2個字串參數的調用
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);
//不帶參數的調用
url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld1";
parameters = new StringDictionary();
message = WebServiceHelper.CallServiceByPost(url, parameters);
System.Console.WriteLine(message);
//帶一個日期文字的調用
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 用戶端通過Http調用請求(轉)