2012-02-16 22 views
1

在我的應用程序中,我發送數據到PHP服務器。我從msdn網站獲得了代碼。如何使用webrequest類方法發送數據?

void SendPost() 
    { 
     var url = "http://www.nuatransmedia.com/ncampaign/mailserver/mail1.php"; 

     // Create the web request object 
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
     webRequest.Method = "POST"; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 

     // Start the request 
     webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);  
    } 

    void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
     // End the stream request operation 
     Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); 

     // Create the post data 
     // Demo POST data 
     string postData = "Hello"; 

     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

     // Add the post data to the web request 
     postStream.Write(byteArray, 0, byteArray.Length); 
     postStream.Close(); 

     // Start the web request 
     webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 
    } 

    void GetResponseCallback(IAsyncResult asynchronousResult) 
    {  
     try 
     { 
      HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
      HttpWebResponse response; 

      // End the get response operation 
      response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamReader = new StreamReader(streamResponse); 
      var Response = streamReader.ReadToEnd(); 
      streamResponse.Close(); 
      streamReader.Close(); 
      response.Close(); 

     } 
     catch (WebException e) 
     { 
      // Error treatment 
      // ... 
     } 
    }` void SendPost() 
    { 
     var url = "http://www.nuatransmedia.com/ncampaign/mailserver/mail1.php"; 

     // Create the web request object 
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
     webRequest.Method = "POST"; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 

     // Start the request 
     webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);  
    } 

    void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
     // End the stream request operation 
     Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); 

     // Create the post data 
     // Demo POST data 
     string postData = "Hello"; 

     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

     // Add the post data to the web request 
     postStream.Write(byteArray, 0, byteArray.Length); 
     postStream.Close(); 

     // Start the web request 
     webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 
    } 

    void GetResponseCallback(IAsyncResult asynchronousResult) 
    {  
     try 
     { 
      HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
      HttpWebResponse response; 

      // End the get response operation 
      response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamReader = new StreamReader(streamResponse); 
      var Response = streamReader.ReadToEnd(); 
      streamResponse.Close(); 
      streamReader.Close(); 
      response.Close(); 

     } 
     catch (WebException e) 
     { 
      // Error treatment 
      // ... 
     } 
    } 

我收到了郵件。但是,正文消息不會顯示在我的郵件中。我在哪裏犯了一個錯誤?在我的PHP代碼中,我使用postData變量來獲取我的消息。

+0

嘗試讀取使用POST數據http://www.codediesel.com/php/reading-raw-post-data-in-php/ – 2012-02-16 14:02:35

回答

0

對於比較簡單的HTTP使用的http://mytoolkit.codeplex.com/wikipage?title=Http類可以幫助你......

這些課程有助於HTTP POST文件(通過$_FILES["..."]訪問)。還有一個RawData屬性(byte[])直接設置數據。

(也GZIP,自己的超時,並且支持更多)

+0

HTTP ://www.dot4pro.com/posting-data-to-the-web-from-windows-phone-7.html-這個網站幫了我很多 – Malarkodi 2012-02-20 06:01:29

相關問題