智者樂山山如畫, 仁者樂水水無涯。 從從容容一盃酒, 平平淡淡一盞茶。 細雨朦朧小石橋, 春風盪漾小竹筏。 夜無明月花獨舞, 腹有詩書气自華。 吾生有崖,也知無崖,以有崖逐無崖,殆也

请求JAVA接口 一直返回 超时

public static string SendJSON(string url, Dictionary<string, string> header, string jsondata)
        {
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (url.Contains("https://"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            //httpRequest.Proxy = new WebProxy("127.0.0.1:8888", true);

            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/json";
            httpRequest.Timeout = 15000;
            foreach (string k in header.Keys)
            {
                httpRequest.Headers.Add(k, header[k]);
            }
            try
            {
                /*解决请求JAVA接口一直返回超时的错误 */
                httpRequest.ServicePoint.Expect100Continue = false;
                byte[] buffer = Encoding.UTF8.GetBytes(jsondata);
                httpRequest.ContentLength = buffer.Length;
                httpRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                string json = reader.ReadToEnd();
                return json;
            }
            catch (WebException ex)
            {
                new internalErrorLog(ex, "发送请求异常");
                httpResponse = (HttpWebResponse)ex.Response;
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                return reader.ReadToEnd();
            }
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

 

request.ServicePoint.Expect100Continue = false;

上面的这一段代码的默认意思是:

是否在请求服务器前,询问是否需要以【100-continue】的形式请求后等待服务器响应

响应通过验证,返回status 100

然而现在是“false” 说明是不需要的询问服务器

问题起因分析:

我一开始请求的代码中压根就没那一行代码,直接套用,发现返回【操作超时】

是因为在请求前先询问是否需要用【100-continue】的形式请求

我默认是“true”,因为不加那行代码默认为“true”

询问发过去,服务器接口压根不处理这个询问

问开发接口的人员说直接在路由层就跳出了

我这边的代码就还在等待询问的响应,等啊等啊【超时操作】就给我报出来了

---------------------------------------------------------------------------------

加了那一行代码后

我就不询问了,直接发送数据

1.服务器收到数据

2.处理

3.响应

正常了!!!

https://cloud.tencent.com/developer/article/1834296

posted @ 2022-04-28 15:42  後生哥哥  阅读(1354)  评论(0编辑  收藏  举报
智者樂山山如畫, 仁者樂水水無涯。 從從容容一盃酒, 平平淡淡一盞茶。 細雨朦朧小石橋, 春風盪漾小竹筏。 夜無明月花獨舞, 腹有詩書气自華。 吾生有崖,也知無崖,以有崖逐無崖,殆也