简体   繁体   English

在 C# 中发送带有请求正文的 WebRequest GET

[英]Send a WebRequest GET with request body in C#

Im trying to request data using GET.我正在尝试使用 GET 请求数据。 I've tried it on Postman, and it already gives expected responses.我已经在 Postman 上尝试过,它已经给出了预期的响应。 在此处输入图像描述 The problem is I cant implement it on C# code because it has request body.问题是我无法在 C# 代码上实现它,因为它有请求正文。

My current code:我当前的代码:

            WebRequest request = WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = CredentialCache.DefaultCredentials;

            //request.Headers.Add("statresp","1");

            WebResponse response = request.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);

            using (Stream dataStream = response.GetResponseStream())
            {
                // Open the stream using a StreamReader for easy access.  
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.  
                string responseFromServer = reader.ReadToEnd();
                // Display the content.  
                Console.WriteLine(responseFromServer);
            }

            // Close the response.  
            response.Close();

Server will gives different response depends on the value of request body.服务器将根据请求正文的值给出不同的响应。 However my current code always gets response as if it was sent without a request body.但是,我当前的代码总是得到响应,就好像它是在没有请求正文的情况下发送的一样。 How to do it without having to switch into POST?如何在不切换到 POST 的情况下做到这一点?

  • look at the network tab on browser查看浏览器上的网络选项卡
  • inspect element F12(the request and response headers).检查元素 F12(请求和响应标头)。

  • add some header to request like cookies or something.添加一些 header 来请求 cookies 之类的。

what is the purpose of this get?这个得到的目的是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM