0

我无法从按钮单击事件的以下代码中获取发布数据和请求标头。这里我必须传递一个 url 和一个字符串作为发布数据...

现在,当我单击按钮时,我应该获取响应标头、请求标头、发布数据和 url 的内容...但我无法从下面的代码中获取请求标头和发布数据...任何人都可以告诉我在哪里我错了

private void button1_Click(object sender, EventArgs e)
            {

                try
                {

                    string url = txtUrl.Text.Trim();

    //HttpWebRequest WebRequestObject = (HttpWebRequest)WebRequest.Create(url);
                   // string result = null;
                    string postData = "This";
                    ASCIIEncoding ObjASCIIEncoding = new ASCIIEncoding();

                    byte[] fileData = ObjASCIIEncoding.GetBytes(postData);

                    // Create a request using a URL that can receive a post. 
                    WebRequest request = WebRequest.Create(url);
                    // Set the Method property of the request to POST.
                    request.Method = "Post";
                    // Create POST data and convert it to a byte array.    
                    WebHeaderCollection webh = request.Headers;
                    txtRequest.Text = webh.ToString();

                    // Set the ContentType property of the WebRequest.
                    request.ContentType = "application/x-wwww-form-urlencoded";
                    // Set the ContentLength property of the WebRequest.
                    request.ContentLength = fileData.Length;
                    // Get the request stream.
                    Stream dataStream = request.GetRequestStream();
                   // StreamReader r = new StreamReader(dataStream);
                   // string p = r.ReadToEnd();
                    // Write the data to the request stream.
                    dataStream.Write(fileData, 0, fileData.Length);
                    // Close the Stream object.
                    dataStream.Close();
                     //Get the response.

                    request.Credentials = CredentialCache.DefaultCredentials;
                 //   HttpWebResponse Response = (HttpWebResponse)WebRequestObject.GetResponse();

                    WebResponse Response = request.GetResponse();
                   // HttpStatusCode code = Response.StatusCode;
                    //txtStatus.Text = code.ToString();
                   txtResponse.Text = Response.Headers.ToString();
                    // Open data stream:
                    Stream WebStream = Response.GetResponseStream();

                    // Create reader object:
                    StreamReader Reader = new StreamReader(WebStream);

                    // Read the entire stream content:

                    txtContent.Text = Reader.ReadToEnd(); 
                    // Cleanup
                    Reader.Close();
                    WebStream.Close();
                    Response.Close();



                    //  var request = WebRequest.Create("http://www.livescore.com ");
                    //var response = request.GetResponse();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
4

1 回答 1

3

这应该得到标题键和值,记住每个键都有一个值数组:

请求标头

要添加标题,请调用Add方法:

    WebRequest request = WebRequest.Create("http://www.google.com");
    request.Method = "GET";
    request.Headers.Add("MyTestHeader", "My Test Header Value");

    foreach (var headerKey in request.Headers.Keys)
    {
        var headerValues = request.Headers.GetValues(headerKey.ToString());
        Trace.TraceInformation("Request Header:{0}, Value:{1}", headerKey, String.Join(";", headerValues));
    }

响应标头

    using (WebResponse response = request.GetResponse())
    {
        foreach (var headerKey in response.Headers.Keys)
        {
            var headerValues = response.Headers.GetValues(headerKey.ToString());
            Trace.TraceInformation("Response Header: {0}, Value: {1}", headerKey, String.Join(";",headerValues));
        }
    }
于 2013-05-02T09:07:17.417 回答