使用Apache HTTP Component将文件上载到box.com

时间:2012-02-25 10:39:14

标签: java http

我正在研究一个java项目,我试图登录到box.com,然后我必须将文件上传到box.com。我正在使用Fiddler分析HTTP POST& Box.com的GET方法。

首次访问box.com,HTTP标头如下

GET http://www.box.com/ HTTP/1.1
Host: www.box.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

,HTTP响应是,

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 25 Feb 2012 00:40:49 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Set-Cookie: z=1qfveojnhh2d3jeemgeo7n22p0; path=/; domain=.box.com; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Set-Cookie: box_visitor_id=4f482e11698672.64923522; expires=Sun, 24-Feb-2013 00:40:49 GMT; path=/; domain=.box.com
Content-Length: 31524

我必须取饼干。

我使用以下代码使用java,

执行相同的操作
System.out.println("Getting upload url from box.com");
    u = new URL("http://www.box.com/");
    uc = (HttpURLConnection) u.openConnection();
    br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    String k = "", tmp;
    while ((tmp = br.readLine()) != null) {
        k += tmp;
    }
Map<String, List<String>> headerFields = uc.getHeaderFields();
if (headerFields.containsKey("Set-Cookie")) {
    List<String> header = headerFields.get("Set-Cookie");
    for (int i = 0; i < header.size(); i++) {
        tmp = header.get(i);
        if (tmp.contains("z=")) {
            zcookie = tmp;
        }
//cookies is a StringBuilder
        cookies.append(tmp).append(";");

    }
}
System.out.println("Cookies : " + cookies);
request_token = parseResponse(k, "request_token = '", "'");
System.out.println("Request token : " + request_token);

要登录,HTTP标头格式为

POST https://www.box.com/login HTTP/1.1
Host: www.box.com
Connection: keep-alive
Content-Length: 218
Cache-Control: max-age=0
Origin: https://www.box.com
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: https://www.box.com/login
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: z=1qfveojnhh2d3jeemgeo7n22p0; box_visitor_id=4f482e11698672.64923522; referrer=https%3A%2F%2Fwww.box.com%2Flogin%2F; __utma=47569716.96461310.1330130513.1330130513.1330130513.1; __utmb=47569716.8.10.1330130513; __utmc=47569716; __utmz=47569716.1330130513.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

此HTTP请求的发布数据如下,

login=someemailid%40yahoo.co.in&password=somepassword&__login=1&reg_step=&submit1=1&folder=&skip_framework_login=&login_or_register_mode=login&new_login_or_register_mode=&request_token=b76216114648bfca8391dfad3cb6ba15

成功登录后,HTTP响应头如下,

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 25 Feb 2012 00:55:05 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Set-Cookie: show_credentials_form=1; expires=Sun, 24-Feb-2013 00:55:00 GMT; path=/; domain=.box.com
Set-Cookie: z=0pueng0cevhsnn4nj6js8g5286; path=/; domain=.box.com; HttpOnly
Set-Cookie: box_user=free; expires=Sat, 03-Mar-2012 00:55:05 GMT; path=/; domain=.box.com
Set-Cookie: partial_id=13493946%2B3n6mk; expires=Sat, 03-Mar-2012 00:55:05 GMT; path=/; domain=.box.com
Set-Cookie: remember_login_off=1; expires=Mon, 28-May-2012 00:55:05 GMT; path=/; domain=.box.com; httponly
Set-Cookie: box_redirect_url=deleted; expires=Fri, 25-Feb-2011 00:55:04 GMT; path=/; domain=.box.com
Set-Cookie: box_redirect_rm=deleted; expires=Fri, 25-Feb-2011 00:55:04 GMT; path=/; domain=.box.com
Content-Length: 555

我做了同样的事情,

HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        System.out.println("Trying to log in to box.com");
        HttpPost httppost = new HttpPost("https://www.box.com/login");
        httppost.setHeader("Cookie", cookies.toString());
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        formparams.add(new BasicNameValuePair("login", "din_ram2007@yahoo.co.in"));
        formparams.add(new BasicNameValuePair("password", ""));
        formparams.add(new BasicNameValuePair("_login", "1"));
        formparams.add(new BasicNameValuePair("reg_step", ""));
        formparams.add(new BasicNameValuePair("submit1", "1"));
        formparams.add(new BasicNameValuePair("folder", ""));
        formparams.add(new BasicNameValuePair("skip_framework_login", ""));
        formparams.add(new BasicNameValuePair("login_or_register_mode", "login"));
        formparams.add(new BasicNameValuePair("new_login_or_register_mode", ""));
        formparams.add(new BasicNameValuePair("request_token", request_token));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);
        System.out.println("response");
        Header[] allHeaders = httpresponse.getAllHeaders();
        for (int i = 0; i < allHeaders.length; i++) {
            System.out.println(allHeaders[i].getName() + " : " + allHeaders[i].getValue());
        }
        System.out.println(EntityUtils.toString(httpresponse.getEntity()));
//        System.exit(0);
        System.out.println("Getting cookies........");
        Iterator<Cookie> it = httpclient.getCookieStore().getCookies().iterator();
        Cookie escookie = null;
        while (it.hasNext()) {
            escookie = it.next();
//            System.out.println(escookie.getName() + " : " + escookie.getValue());

            cookies.append(escookie.getName()).append(" = ").append(escookie.getValue()).append(";");
        }

        String tmp = cookies.toString();
        tmp = tmp.replace(zcookie, "");
        cookies.setLength(0);
        cookies = cookies.append(tmp);
        System.out.println(cookies.toString());

这里,在执行代码之后,我可以获得除'box_redirect_url'和'box_redirect_rm'cookie之外的所有cookie。

我不知道代码有什么问题。

顺便说一句,在成功登录后,该页面将被重定向到“https://www.box.com/files”,我必须使用我从一开始就得到的cookie获取该页面。

HTTP标头格式如下

GET https://www.box.com/files HTTP/1.1
Host: www.box.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: https://www.box.com/login
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: box_visitor_id=4f482e11698672.64923522; referrer=https%3A%2F%2Fwww.box.com%2Flogin%2F; __utma=47569716.96461310.1330130513.1330130513.1330130513.1; __utmb=47569716.8.10.1330130513; __utmc=47569716; __utmz=47569716.1330130513.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); show_credentials_form=1; z=0pueng0cevhsnn4nj6js8g5286; box_user=free; partial_id=13493946%2B3n6mk; remember_login_off=1

我尝试了以下内容,

System.out.println("Getting request token & real time subscriber id ");
        u = new URL("https://www.box.com/files/");
        uc = (HttpURLConnection) u.openConnection();
        uc.setRequestProperty("Cookie", cookies.toString());
        br = new BufferedReader(new InputStreamReader(uc.getInputStream()));

        String k = "", tmp;
        while ((tmp = br.readLine()) != null) {
            System.out.println(tmp);
            k += tmp;
        }

        request_token = parseResponse(k, "request_token = '", "'");
        System.out.println("Request token : " + request_token);
        subscriber_id = parseResponse(k, "realtime_subscriber_id ='", "'");

这是我真正遇到问题的地方。成功登录后,我请求的页面不是它的样子。相反,它就像我第一次访问这个网站。每一次,我都在查看cookies&amp;为HTTP POST&amp;正确设置它们得到。但是我没有得到我打算得到的东西。

代码有什么问题?任何人都可以解释这个问题。 (请耐心等待我提出这个大问题,因为我认为这可能是解释我问题的明确方法)

0 个答案:

没有答案