2012-02-16 16 views
1

私のアプリケーションでは、PHPサーバーにデータを送信します。 msdnのWebサイトからコードを入手しました。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を使用します。

http://mytoolkit.codeplex.com/wikipage?title=Httpのクラスはあなたを助けるかもしれない単純なHTTPの使用方法について
+0

http://www.codediesel.com/php/reading-raw-post-data-in-php/ –

答えて

0

...

これらのクラスは、HTTP POSTファイル($_FILES["..."]経由でアクセス)に役立ちます。また、データを直接設定するためのRawDataプロパティ(byte[])があります。

(もGZIP、自身のタイムアウト、およびそれ以上はサポートされています)

+0

HTTPを使用してPOSTデータの読み込みお試しください://www.dot4pro.com/posting-data-to-the-web-from-windows-phone-7.html-このウェブは私をたくさん助けました – Malarkodi

関連する問題