Windows 如何使用webrequest类方法发送数据?

Windows 如何使用webrequest类方法发送数据?,windows,windows-phone-7,webrequest,sendmessage,Windows,Windows Phone 7,Webrequest,Sendmessage,在我的应用程序中,我将数据发送到PHP服务器。我从msdn网站上得到了代码 void SendPost() { var url = "http://www.nuatransmedia.com/ncampaign/mailserver/mail1.php"; // Create the web request object HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Crea

在我的应用程序中,我将数据发送到PHP服务器。我从msdn网站上得到了代码

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使用,中的类可能会帮助您

这些类有助于通过$_files[…]访问HTTP POST文件。还有一个RawData属性byte[]可以直接设置数据

此外,还支持GZIP、own timeout等功能

尝试使用web读取帖子数据对我帮助很大