引用System.Net、System.IO名稱空間。
Method:傳遞方法
ContentType:內容類型
Url:網址
Param:傳遞參數
message:回傳結果
public static string CallWeb(string Method, string ContentType, string Url, string Param)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.ContentType = ContentType;
request.Method = Method;
if (Method.ToUpper().Equals("POST") && !string.IsNullOrEmpty(Param))
{
using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(Param);
streamWriter.Flush();
streamWriter.Close();
}
}
string message = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
message = streamReader.ReadToEnd();
}
return message;
}
沒有留言:
張貼留言