与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;
}
}