1. 程式人生 > 實用技巧 >c# get post請求

c# get post請求

using System;
using System.IO;
using System.Net;
using System.Text;

public class MyWebRequest
{
    private WebRequest request;
    private Stream dataStream;

    private string status;

    public String Status
    {
        get
        {
            return status;
        }
        set
        {
            status 
= value; } } /// <summary> /// 直接請求的 /// </summary> /// <param name="url"></param> public MyWebRequest(string url) { // Create a request using a URL that can receive a post. request = WebRequest.Create(url); } /// <summary> ///
不帶引數的get/post /// </summary> /// <param name="url"></param> /// <param name="method"></param> public MyWebRequest(string url, string method) : this(url) { if (method.Equals("GET") || method.Equals("POST")) { // Set the Method property of the request to POST.
request.Method = method; } else { throw new Exception("Invalid Method Type"); } } /// <summary> /// 帶引數的get/post /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="data"></param> /// <param name="contentType">用下面的HttpContentType類中的常量</param> public MyWebRequest(string url, string method, string data, string contentType) : this(url, method) { // Create POST data and convert it to a byte array. string postData = data; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. //request.ContentType = "application/x-www-form-urlencoded"; request.ContentType = contentType; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); } public string GetResponse() { WebResponse response = request.GetResponse(); this.Status = ((HttpWebResponse)response).StatusDescription; dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); //釋放 reader.Close(); dataStream.Close(); response.Close(); return responseFromServer;//返回資料 } /// <summary> /// HTTP 內容型別(Content-Type) /// </summary> public class HttpContentType { /// <summary> /// 資源型別:普通文字 /// </summary> public const string TEXT_PLAIN = "text/plain"; /// <summary> /// 資源型別:JSON字串 /// </summary> public const string APPLICATION_JSON = "application/json"; /// <summary> /// 資源型別:未知型別(資料流) /// </summary> public const string APPLICATION_OCTET_STREAM = "application/octet-stream"; /// <summary> /// 資源型別:表單資料(鍵值對) /// </summary> public const string WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; /// <summary> /// 資源型別:表單資料(鍵值對)。編碼方式為 gb2312 /// </summary> public const string WWW_FORM_URLENCODED_GB2312 = "application/x-www-form-urlencoded;charset=gb2312"; /// <summary> /// 資源型別:表單資料(鍵值對)。編碼方式為 utf-8 /// </summary> public const string WWW_FORM_URLENCODED_UTF8 = "application/x-www-form-urlencoded;charset=utf-8"; /// <summary> /// 資源型別:多分部資料 /// </summary> public const string MULTIPART_FORM_DATA = "multipart/form-data"; } }