【问题标题】:HTTP URL Connection TimeoutHTTP URL 连接超时
【发布时间】:2016-09-26 16:32:14
【问题描述】:

当我在浏览器中点击以下 URL 时,它运行良好:

http://62.215.226.164/fccsms_P.aspx?UID=something&P=something&S=InfoText&G=96567771404&M=hello&L=E

但是当我尝试使用以下 java 代码访问 URL 时,它不起作用:

try {
        URL url = new URL(null, "http://62.215.226.164/fccsms_P.aspx?", new sun.net.www.protocol.https.Handler());
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

        con.setRequestMethod("POST");

        String urlParameters="UID=something&P=something=InfoText&G=96567771404&M=hello&L=E";
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
        }
        in.close();
    } 
     catch(Exception e) {
        System.err.println("Error: "+e.getMessage());   
    }

代码有问题吗?

【问题讨论】:

  • 究竟是什么不工作?
  • @SomePerson 无法点击网址
  • “点击”网址到底是什么意思?程序在做什么错误
  • 程序应该向它不这样做的 URL 发出一个 http post 请求
  • 是否抛出异常?它做错了什么吗?你真的需要更具体。

标签: java http httpurlconnection


【解决方案1】:

您正在通过 HttpsURLConnection 发送 HTTP url。所以它的制作问题。只需更改您的前 2 行。你会得到正确的输出。

URL url = new URL("http://62.215.226.164/fccsms_P.aspx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();

【讨论】:

  • 代码转储不是一个好的答案。它要求读者将其与原始问题的代码逐行进行比较。解释您所做的更改并解释原因。
【解决方案2】:

您需要先修复您的请求参数:

urlParameters="UID=something&P=something=InfoText&G=96567771404&M=hello&L=E";

请在此处发布异常堆栈跟踪以更好地帮助您。

【讨论】:

    猜你喜欢
    • 2014-03-11
    • 2011-03-10
    • 2011-06-02
    • 2021-11-11
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2014-03-08
    相关资源
    最近更新 更多