简体   繁体   English

Java HttpURLConnection发布方法不起作用

[英]Java HttpURLConnection post method not working

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. 我设置标题,然后获取输出流并写入一些字节,然后关闭输出流。

I am trying to get the next page for schedule details from given url. 我正在尝试从给定的URL获取下一页的时间表详细信息。 But some how i am not getting the result. 但是有些我没有得到结果。 Please help anybody if you know any issue in this code. 如果您知道此代码中的任何问题,请帮助任何人。

I am getting first page with error instead of second page. 我的首页出现错误,而不是第二页。 "The station combination you have chosen is invalid. Please call the LIRR Travel Information Center at (718) 217-5477 and ask for a representative for more information." “您选择的车站组合无效。请致电LIRR旅行信息中心,电话为(718)217-5477,并要求代表提供更多信息。”

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

You are missing the "?" 您错过了“?” Between index.php and the parameters. 在index.php和参数之间。

You should make 你应该做

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

into

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

HttpURLConnection.getResponseCode();

this method may help you. 此方法可能会帮助您。
It forces the connection really establish the connection. 它强制连接真正建立连接。
may be you want to use Apache HttpClient to make your life easier... 可能是您想使用Apache HttpClient使您的生活更轻松...

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

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