繁体   English   中英

Java:我认为我为请求创建的有效负载是错误的。 它在 Java 中给出错误连接超时

[英]Java: I think I created payload for request is wrong. it is giving error Connection timed out in Java

Java:我认为我为请求创建的有效负载是错误的。 它在 Java 中给出错误连接超时

我收到以下错误。 java.net.ConnectException:连接超时 我认为我创建的有效载荷是错误的。 我使用以下 Curl 创建了有效负载。 请帮忙。

curl --location --request POST 'https://Vishal.net/v1/oauth20/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_secret=d4j8Q~po9Tm3PEShtcHcaeBZW1cuYtHKcbA0FcwG' \
--data-urlencode 'client_id=9572e860-7dd7-45ff-a9c7-e9c09a915a4c' \
--data-urlencode 'scope=api://e3981169-cbc9-4e08-aa35-620714eab5bb/.default'

公共字符串 getToken() 抛出 IOException, ServletException{

            String output = "";
            URL url = null;
            try {
                url = new URL(baseUrl+tokenSubpath);
                createLogFile(baseUrl+tokenSubpath);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                return"Error: "+ e.toString();
            }
            System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
            HttpURLConnection connection = null;
            try {
                //If we need to connect through proxy uncomment the below line
                connection = (HttpURLConnection) url.openConnection();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return "Error: "+e.toString();
            }
            connection.setInstanceFollowRedirects(false);
            connection.setDoOutput(true); // Triggers POST.
            try {
                connection.setRequestMethod("POST");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return "Error: "+e.toString();
            }
            String Bearer = new String(Base64.getEncoder().encode(authUserPass.getBytes()));
              //connection.setRequestProperty("Authorization", "Basic "+Bearer); //vishal bedre 8/8/2022
            connection.setRequestProperty("Authorization", "");
            
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Length", "");  //vishal bedre 8/8/2022
            connection.setRequestProperty("Host", "");  //vishal bedre 8/8/2022
            connection.setRequestProperty("Accept", "*/*");  //vishal bedre 8/8/2022
            connection.setUseCaches(false);
            
            String urlParameters="grant_type=client_credentials&client_secret=d4j8Q~po9Tm3PEShtcHcaeBZW1cuYtHKcbA0FcwG&client_id=9572e860-7dd7-45ff-a9c7-e9c09a915a4c&scope=api://e3981169-cbc9-4e08-aa35-620714eab5bb/.default";
            

            
            try(OutputStream os = connection.getOutputStream()) {
                //os.write(postData);
                OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");    
                //osw.write("grant_type=client_credentials");
                osw.write(urlParameters);
                osw.flush();
                osw.close();
                os.close();  //don't forget to close the OutputStream
                connection.connect();           
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return "Error: "+e.toString();
            }
            

您需要查看数据是如何通过 http/https 传输的。

  1. 客户端首先从 URL 中找出主机名和端口。
  2. 它尝试将主机名解析为 IP。
  3. 它创建一个到远程 ip:port 的 TCP 连接
  4. 如果 https 正在使用中,则进行证书检查、身份验证和加密协商
  5. HTTP 启动,客户端发送请求(包括有效负载)
  6. 服务器发送 http 响应

您正在考虑第 5 步中的问题,而您的异常说明了第 3 步中的问题。所以忘记有效负载并检查为什么会出现超时。 也许您需要打开防火墙。

暂无
暂无

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

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