2011-09-05 29 views
1

我正在向POST簡單服務器示例發送POST數據。在頭文件中,我還有一些其他細節,這些細節作爲輸入流打包到POST數據發送中。如何使用HandlePostRequest檢索它們?我的源代碼附在這裏:.NET handlepostrequest - 檢索數據

public void handlePOSTRequest() { 

     Console.WriteLine("get post data start"); 
     int content_len = 0; 
     MemoryStream ms = new MemoryStream(); 
     if (this.httpHeaders.ContainsKey("content-length")) { 
      content_len = Convert.ToInt32(this.httpHeaders["content-length"]); 
      if (content_len > MAX_POST_SIZE) { 
       throw new Exception(
        String.Format("POST Content-Length({0}) too big for this simple server", 
         content_len)); 
      } 
      byte[] buf = new byte[BUF_SIZE]; 

      int to_read = content_len; 
      while (to_read > 0) { 
       Console.WriteLine("starting Read, to_read={0}",to_read); 
       int numread = this.inputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read)); 

       Console.WriteLine("read finished, numread={0}", numread); 
       if (numread == 0) { 
        if (to_read == 0) { 
         break; 
        } else { 
         throw new Exception("client disconnected during post"); 
        } 
       } 
       to_read -= numread; 
       ms.Write(buf, 0, numread); 
      } 
      ms.Seek(0, SeekOrigin.Begin); 
     } 
     else 
     { 
      Console.WriteLine("Missing content length"); 
     } 
     Console.WriteLine("get post data end"); 
     srv.handlePOSTRequest(this, new StreamReader(ms)); 

    } 

我得到的一切都是content_length,但我需要從流中獲取數據。該流由inputStream = new BufferedStream(socket.GetStream())收集;而在這個流中,我有一個值「註冊」=「123456789」,如何檢索它?

謝謝

回答

1

你在這裏。

string data; 
using (var streamReader = new StreamReader(Request.InputStream)) 
{ 
    data = streamReader.ReadToEnd(); 
} 

雖然如果你只需要的是registration

var registration = Request["registration"]; 

一切都基本上上Request實例,它可以從一個PageWebControl,或HttpContext.Current.Request訪問。在HttpHandler的情況下,將爲您傳入HttpContext實例。

public void ProcessRequest(HttpContext context) 
{ 
    ... 
}