c#(HttpWebResponse)webRequest.GetResponse() 超时问题

与WMS进行接口测试,用postman发送post请求测试接收数据没问题,
我自己写的代码发送POST请求在调试GetResponse()的时候一直报请求超时,
然后PING了一下主机发现请求超时,还以为GetResponse()源码实现原理是要先Ping主
机…,但是我用Get请求接口能返回错误信息,所以基本就排除GetResponse()源码的问题了,
回到参数设置那里,在网上对比所有要设置的参数,看我的参数是不是漏设置了。
最后修改这句代码

webRequest.ProtocolVersion = HttpVersion.Version10;//这里默认是Version11

在这里插入图片描述
最后终于接收到数据了,原来是HTTP 版本号的问题,吐血。。。

private WaveDetaildata RequestInterface(string url, string pallet, string Operator, int pickNumber, string specialBarCode)
        {
            Uri _url = new Uri(url);
            string _specialBarCode = string.IsNullOrEmpty(specialBarCode) ? "N" : "Y";
            string json = "{" +$"\"pallet\": \"{pallet}\",\"operator\":\"{Operator}\",\"pickNumber\":\"{pickNumber}\",\"specialBarCode\":\"{ _specialBarCode }\"" + "}";
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_url);

            try
            {
                
                webRequest.Method = "POST";
                webRequest.Headers.Add(HttpRequestHeader.Authorization, "test1");
                webRequest.ContentType = "application/json";
                webRequest.Accept = "text/html, application/xhtml+xml, */*";
                //webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                //webRequest.KeepAlive = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.CheckCertificateRevocationList = true;
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webRequest.Referer = url;
                webRequest.ProtocolVersion = HttpVersion.Version10;
                webRequest.MaximumAutomaticRedirections = 4;
                webRequest.MaximumResponseHeadersLength = 4;

                //webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
                

               
                byte[] byt = Encoding.Default.GetBytes(json);
                webRequest.ContentLength = byt.Length;
                Stream stream = webRequest.GetRequestStream();
                stream.Write(byt, 0, byt.Length);
                stream.Close();
              

                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                string responseFormat = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                WaveDetaildata waveDetaildata = (WaveDetaildata)JsonConvert.DeserializeObject(responseFormat);
                return waveDetaildata;
            }
            catch (WebException ex)
            {
                webRequest.Abort();
                MessageBox.Show(ex.Message);
                //var res = (HttpWebResponse)ex.Response;
                //StringBuilder sb = new StringBuilder();
                //StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                //sb.Append(sr.ReadToEnd());
                //MessageBox.Show(sb.ToString());
                return null;
            }

        }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值