在postman接口调试工具中有结果返回,在后台代码中却没有结果返回,求指点

首先我使用postman测试工具,测试接口,返回结果正常,如下图

img


然而,我使用c#后台代码去访问这个接口,却不能获取返回值,最终的返回结果却是空字符串,代码如下

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream streamResponse = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create("http://192.168.2.20:8080/api/v1/auth/login");
                request.Method = "Post";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.KeepAlive = true;
                request.ProtocolVersion = HttpVersion.Version10;

                string jsonContract = "{ \"password\" : \"ZJHzjh800521@1\"}";
                byte[] PostData = Encoding.UTF8.GetBytes(jsonContract);
                //发送数据  using结束代码段释放
                using (Stream requestStm = request.GetRequestStream())
                {
                    requestStm.Write(PostData, 0, PostData.Length);
                }
                ServicePoint currentServicePoint = request.ServicePoint;
                currentServicePoint.ConnectionLimit = 1000;

                //响应
                response = (HttpWebResponse)request.GetResponse();
                streamResponse = response.GetResponseStream();
                string strResponse = string.Empty;
                //如果服务端采用了GZIP压缩,则先解压缩。
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    using (GZipStream gz = new GZipStream(streamResponse, CompressionMode.Decompress))
                    {
                        using (StreamReader readerGzip = new StreamReader(gz, Encoding.UTF8))
                        {
                            strResponse = readerGzip.ReadToEnd();
                        }
                    }
                }
                else
                {
                    using (StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8))
                    {
                        strResponse = streamRead.ReadToEnd();
                    }
                }
                return strResponse;**//这里的值最后还是空的**
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return msg;
            }

请教:我的代码是在哪里出问题了呢,或者还有什么地方没有设置好?怎样才能实现像postman的那种效果,成功返回结果

有个很简单的小技巧,postman可以快速提供多种代码生成
使用方法:
第一步:打开代码生成器

img


第二步:选择需要生成的代码类型

img

img

request = (HttpWebRequest)WebRequest.Create("http://192.168.2.20:8080/api/v1/auth/login");
                request.Method = "Post";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.KeepAlive = true;
                request.ProtocolVersion = HttpVersion.Version10;

head传的不是json

 request.ContentType = "application/json; charset=UTF-8";

用fillder或者mitmproxy抓包看下两个请求有什么不同,然后改代码到请求一样,应该可以解决问题

Time,您好,我把head换成application/json也是不行,我已经换了好几种ContentType了

1.postman返回正常说明接口整体逻辑没问题。
2.先说下java中文件流的使用,供参考,文件流有一个特点,就是这个流是一次性的,一旦使用就没有了这次就不能用了,也就是在后台代码调试过程中,想要打印出值,需要再copy一份用于打印。
3.你这个

strResponse = streamRead.ReadToEnd();

应该是一个指针在流中的位置的问题,当执行这个方法的时候,指针就设置到了流的末尾。所以你要在执行这个方法之前把指针设置到开头的位置。

someStream.Seek(0, SeekOrigin.Begin);

希望这些思路能帮到你

看一下postman里面header(11)里面的设置,加全了再试一下。看看是不是有其他漏掉的设置。

用Fiddler抓包看下postman发送的请求头有什么,C#按照postman的请求头一起加上,postman的发送的请求不是键值对(application/x-www-form-urlencoded),而是raw,所以C#设置application/x-www-form-urlencoded无效。

可能是缺少user-agent,将你浏览器的useragent拷贝出来附带上试试

我们的套路:
postman连接,wireshark抓包得到包1 ;
你的c#程序连接,wireshark抓包得到包2;
然后对比抓包文件里的请求字段分析差异内容即可。

希望对您有用。

还要注意http和https的区别。

各位好,我利用postman快速生成的代码,粘贴到c#环境中竟然成功了.
这种post方式的是没有问题了,但是却遇到了另外一个问题:
取数据方式,即GET方式却不行,报404错误,但是在postman中是可以的,
而且我也是原封不动地将代码拷贝过去的.
代码如下:

var client = new RestClient("http://192.168.2.154:8080/api/v1/passlogs?startTime=1631430506122&endTime=1631432506127&resultType=0&page=0&size=1");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Cookie", "__SESSION_ID__=456DDC09B54F00F72709FDF2");
var body = @"{
" + "\n" +
@"    ""password"": ""ZJHzjh800521@1""
" + "\n" +
@"}";
request.AddParameter("application/json", body,  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

上面的代码在postman中一点问题都没有,但是放在c#中却报401错误,这如何解决呢?求指教

而且我把请求地址粘贴到浏览器中是有返回结果的,也是正常的

img


但是在代码中报401,应该是权限问题