Java HttpURLConnection post方法不起作用

时间:2013-08-24 20:57:04

标签: java post httpurlconnection

我正在使用HttpURLConnection通过POST将数据发送到服务器。我设置标题,然后获取输出流并写入一些字节,然后关闭输出流。

我正在尝试从给定的网址获取下一页的计划详细信息。但有些我怎么没有得到结果。如果您对此代码中的任何问题有任何疑问,请帮助任何人。

我收到错误而不是第二页的第一页。 “您选择的电台组合无效。请拨打(718)217-5477致电LIRR旅游信息中心,并要求代表提供更多信息。”

     public static void main(String[] args) throws Exception {
        String url = "http://lirr42.mta.info";
        String cookie = retrieveCookies(url);
        String urlParameters = "FromStation=56&ToStation=8&RequestDate=08/24/2013&RequestTime=01:00&RequestAMPM=PM&sortBy=1&schedules=schedules";
        String page = postHttpPage(url + "/index.php", 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();

        StringBuffer response = new StringBuffer();
        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; 
    }  

3 个答案:

答案 0 :(得分:1)

你错过了“?”在index.php和参数之间。

答案 1 :(得分:0)

你应该

String urlParameters = "FromStation=56&ToStation=8&RequestDate=08/24/2013&RequestTime=01:00&RequestAMPM=PM&sortBy=1&schedules=schedules";

String urlParameters = "?FromStation=56&ToStation=8&RequestDate=08/24/2013&RequestTime=01:00&RequestAMPM=PM&sortBy=1&schedules=schedules";

答案 2 :(得分:0)

HttpURLConnection.getResponseCode();

这种方法可以帮到你。
它强制连接真正建立连接 可能你想使用Apache HttpClient让你的生活更轻松......