Get是用來從服務(wù)器上獲得數(shù)據(jù),而Post是用來向服務(wù)器上傳遞數(shù)據(jù).
HTTP同步請求
HTTP異步請求
 Handler h = new Handler();  AsyncCallback callback = new AsyncCallback(h.Callback); //方法
//將請求對象作為狀態(tài)對象傳遞
 req.BeginGetResponse(callback, req);
回調(diào)函數(shù),用類來表示
public class Handler  { public void Callback(IAsyncResult ar)  { //將Requeststate對象強(qiáng)制轉(zhuǎn)化為webRequest對象
 HttpWebRequest req = (HttpWebRequest)ar.AsyncState;  //得到與這個(gè)請求相關(guān)的響應(yīng)對象
 HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(ar);  //開始從響應(yīng)流中讀取數(shù)據(jù)
 Stream s = res.GetResponseStream();   StreamReader sr = new StreamReader(s, System.Text.Encoding.ASCII);  System.Text.StringBuilder sb = new System.Text.StringBuilder();  char[] data = new char[1024];  int nBytes;  do {  nBytes = sr.Read(data, 0, ( int)1024);  sb.Append(data);  } while (nBytes == 1024);   }  }
|