HTTP URL连接超时

时间:2016-09-26 16:32:14

标签: java http httpurlconnection

当我在浏览器中点击以下网址时,它运行正常:

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());   
    }

代码中有什么问题吗?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

urlParameters = “UID =东西&P=something=InfoText&安培; G = 96567771404&安培; M =&你好放大器; L = E”;

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