简体   繁体   English

Java HttpURLConnection post方法将参数传递给cgi

[英]Java HttpURLConnection post method Passing Parameters to cgi

I am a newbie in Java web programming. 我是Java Web编程的新手。 I am using HttpURLConnection to send data to a server via POST. 我正在使用HttpURLConnection通过POST将数据发送到服务器。 I set the headers then get the output stream and write some bytes then close the output stream. 我设置标题,然后获取输出流并写入一些字节,然后关闭输出流。 As far as I know it is the correct way but the server send me an unknown exception. 据我所知,这是正确的方法,但是服务器向我发送了一个未知异常。

Could you give me some hint why this exception happens 你能给我一些提示为什么会发生这种异常

 public static void main(String[] args) throws Exception {
        String url = "http://www.nlm.nih.gov/cgi/mesh/2014/MB_cgi";
        String cookie = retrieveCookies(url);
        String urlParameters = "?term=Cancer&exact=Find Exact Term&field=all";
        String page = postHttpPage(url , urlParameters, cookie);
        System.out.println(page);
        System.out.println();
    }

    public static String postHttpPage(String url, String urlParameters, String cookie) throws Exception {
        System.out.println("\nSending 'POST' request to URL : " + url);
        URL obj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        return postPage(conn, urlParameters, cookie);
    }

    private static String postPage(HttpURLConnection conn, String urlParameters, String cookie) throws Exception {
        conn.setRequestMethod("POST");
        conn.setRequestProperty("User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes().length));
        conn.setRequestProperty("Content-Language", "en-US");
        conn.setRequestProperty("Cookie", cookie);
        //conn.setInstanceFollowRedirects(true);
        // Send post request
        //conn.setDoInput(true);
        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(urlParameters);
        //System.out.println("wr : " + wr.size());
        wr.flush();
        wr.close();

        StringBuilder response = new StringBuilder();
        int responseCode = conn.getResponseCode();
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }

    public static String retrieveCookies(String url) throws IOException{  
        URL obj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        String cookies=conn.getHeaderField("Set-Cookie");  
        System.out.print("cookies: " + cookies);
        conn.disconnect();
        return cookies; 
    }  

The problem is about the server. 问题出在服务器上。 The server did not respond you if you send many requests and also sometimes it does not work might be due to the high amount of requests. 如果您发送许多请求,服务器没有响应您,有时由于大量请求,它也无法正常工作。

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

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