有路过的cookies问题GET请求(POST之后) [英] Issues with passing cookies to GET request (after POST)

查看:237
本文介绍了有路过的cookies问题GET请求(POST之后)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我停留在这个问题上了好几天了,我的眼睛也开始不时伤花了尝试不同的组合,但没有成功。关键是,我想提出一个应用程序,它已经得到的数据形成了互联网,解析它,然后显示给用户。我试过几种方法来这样做,并使用JSOUP是非常有益的,特别是分析和获取数据出来的结果。

I am stuck on this issue for several days now, my eyes are starting to hurt from time spent trying different combinations, but without success. The thing is, I am making an app, which has to get data form the internet, parse it and then show it to the user. I've tried several methods for doing that, and using JSOUP was very helpful, especially with parsing and getting the data out of the results.

但是,我解决不了一个问题。我曾尝试与普通HttpClient和与JSOUP但我不能顺利拿到我所需要的数据。这是我的code(JSOUP版):

However, there is one issue which I can not resolve. I have tried with the regular HTTPClient, and with JSOUP but I can't successfully get the data I need. Here is my code (JSOUP version):

public void bht_ht(Context c, int pozivni, int broj) throws IOException {
    //this is the first connection, to get the cookies (I have tried the version without this method separate, but it's the same
    Connection.Response resCookie = Jsoup.connect("http://www.bhtelecom.ba/imenik_telefon.html")
            .method(Method.GET)
            .execute();
    String sessionId = resCookie.cookie("PHPSESSID");
    String fetypo = resCookie.cookie("fe_typo_user");
    //these two above are the cookies

    //the POST request, with the data asked
    Connection.Response res = Jsoup.connect("http://www.bhtelecom.ba/imenik_telefon.html?a=search")
              .data("di", some_data)
              .data("br", some_data)
              .data("btnSearch","Tra%C5%BEi")
              .cookie("PHPSESSID", sessionId)
              .cookie("fe_typo_user", fetypo)
              .method(Method.POST)
              .execute();

    Document dok = res.parse();

            //So, here is the GET request for the site which contains the results, and this site is redirected to with HTTP 302 response after the POSt result
    Document doc = Jsoup.connect("http://www.bhtelecom.ba/index.php?id=3226&")
            .cookie("PHPSESSID", sessionId)
            .cookie("fe_typo_user", fetypo)
            .referrer("http://www.bhtelecom.ba/imenik_telefon.html")
           .get();

    Document doc = res2.parse();

    Element elemenat = doc.select("div.boxtexter").get(0);

   String ime = elemenat.text();

}

因此​​,最终的结果将是,其中包含了返回数据的串。但是,无论我尝试,我得到了空白网页,它的解析的文本,我也模拟了该浏览器请求的一切。

So, the end result would be a string which contains the returned data. But, whatever I try I get the "blank" page and it's parsed text, and I've simulated everything which is requested by the browser.

下面是POST和GET通过浏览器捕获的原始标题: (POST)

Here are the POST and GET raw headers captured by the browser: (post)

> POST /imenik_telefon.html?a=search HTTP/1.1 Host: www.bhtelecom.ba
> Content-Length: 56 Cache-Control: max-age=0 Origin:
> http://www.bhtelecom.ba User-Agent: Mozilla/5.0 (Windows NT 6.1;
> WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202
> Safari/535.1 Content-Type: application/x-www-form-urlencoded Accept:
> text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
> Referer: http://www.bhtelecom.ba/index.php?id=3226& 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:
> PHPSESSID=opavncj3317uidbt93t9bie980;
> fe_typo_user=332a76d0b1d4944bdbbcd28d63d62d75;
> __utma=206281024.1997742542.1319583563.1319583563.1319588786.2; __utmb=206281024.1.10.1319588786; __utmc=206281024; __utmz=206281024.1319583563.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
> 
> di=033&br=123456&_uqid=&_cdt=&_hsh=&btnSearch=Tra%C5%BEi

(获取)

> GET /index.php?id=3226& HTTP/1.1 Host: www.bhtelecom.ba Cache-Control:
> max-age=0 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64)
> AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1
> Accept:
> text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
> Referer: http://www.bhtelecom.ba/index.php?id=3226& 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:
> PHPSESSID=opavncj3317uidbt93t9bie980;
> __utma=206281024.1997742542.1319583563.1319583563.1319588786.2; __utmb=206281024.1.10.1319588786; __utmc=206281024; __utmz=206281024.1319583563.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); fe_typo_user=07745dd2a36a23c64c2297026061a2c2

在此得到的,(它的反应),我需要的数据的位置,但随着参数,饼干的任意组合,或一切,我试过了,我不能得到它认为我发了一个帖子,现在希望这些数据。

In this GET, (its response), the data I need is located, but with any combination of parameters, cookies, or everything I tried, I couldn't get it to "think" that I made a POST and now want that data.

下面是没有JSOUP解析器我的code版本,但我不能得到它的工作要么,虽然当我检查这些cookie,他们都OK,相同的POST和GET,但没有成功。

Here is the version of my code without JSOUP parser, but I can't get it to work either, although when I check those cookies, they are OK, same for POST and GET, but without success.

DefaultHttpClient client = new DefaultHttpClient();


                 String postURL = "http://www.bhtelecom.ba/imenik_telefon.html?a=search";
                 HttpPost post = new HttpPost(postURL);
                 post.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);


                 List<NameValuePair> params = new ArrayList<NameValuePair>();
                 params.add(new BasicNameValuePair("di", "035"));
                 params.add(new BasicNameValuePair("br", "819443"));
                 params.add(new BasicNameValuePair("btnSearch","Tra%C5%BEi"));
                 UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
                 post.setEntity(ent);
                 HttpResponse responsePOST = client.execute(post);



                 HttpEntity resEntity = responsePOST.getEntity();  

                 if (resEntity != null) {    
                    //todo
                     }
                 //checking for cookies, they are OK
                 List<Cookie> cookies = client.getCookieStore().getCookies();
                 if (cookies.isEmpty()) {
                      Log.d(TAG, "no cookies");
                 } else {
                     for (int i = 0; i < cookies.size(); i++) {
                          Log.d(TAG, "cookies: " + cookies.get(i).toString());
                     }
                 }
                 resEntity.consumeContent();

                 HttpGet get = new HttpGet("http://www.bhtelecom.ba/index.php?id=3226&");
                 get.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);



                 HttpResponse responseGET = client.execute(get);
                 HttpEntity entityGET = responseGET.getEntity();
                 List<Cookie> cookiesGet = client.getCookieStore().getCookies();
                 if (cookies.isEmpty()) {
                      Log.d(TAG, "no cookies");
                 } else {
                     for (int i = 0; i < cookiesGet.size(); i++) {
                          Log.d(TAG, "cookies GET: " + cookiesGet.get(i).toString());
                     }
                 }

                 //a method to check the data, I pass the InputStream to it, and do the operations, I've tried "manually", and passing the InputStream to JSOUP, but without success in either case.
                 samplemethod(entityGET.getContent());
                 client.getConnectionManager().shutdown();
             } catch (Exception e) {
                 e.printStackTrace();
             }  

因此​​,如果任何人都可以在我的设置发现错误,或者我找一个方法,使这两个请求,然后获取数据,HTTP实体,我可以再为输入(InputStream中)用来可爱JSOUP分析器,这将是惊人的。或者,也许我这是什么做的页面需要整个事情,我需要使用不同的参数让我的请求,我会AP preciate的。我使用Wireshark的和查尔斯调试代理来理解这个概念来创造什么(都试过,要仔细检查),发现只有会话ID,fe_typo_user并用于跟踪网站停留时间等其他一些参数,我已经试过通过他们太多,_ UTMA的_utmb......等等。

So, if anyone can find an error in my set up, or find me a way to make these two requests and then get the data, HTTP Entity, which I could then use as an input (InputStream) to lovely JSOUP parser, that would be amazing. Or maybe I got this whole thing about what does the page need, and I need to make my requests with different parameters, I would appreciate that. I used Wireshark and Charles Debugging Proxy to get the idea what to create (tried both, to double check), and found only that session id, fe_typo_user and some other parameters used for tracking the time on site and etc, and I've tried passing them too, "_utma" "_utmb" ... and so on.

我有一些其他的方法,用简单,POST只有响应数据的方法,我已经成功地得到了这一点,但与此网站这一具体问题是让我疯了。在此先感谢您的帮助。

I have some other methods, using "simpler", POST only methods with data in response, and I've successfully got that, but this specific issue with this site is driving me crazy. Thanks in advance for your help.

推荐答案

很多很多个小时的尝试新事物和跟踪流入/流出数据包后,我终于找到一个解决方案。

After many, many hours of trying things and tracking incoming/outgoing packets, I finally managed to find a solution.

的事情是与错误,还是的HttpClient的行为。如果一个参数添加到一个职位,一个参数是emty,有的价值,它不与发送请求。我不知道,并认为这些参数,因为它们是空的,不会改变enything,并与做的东西与JSOUP我并没有将它们传递到请求。

The things was with the "bug", or the behavior of HTTPClient. If you add a parameter to a post, and a parameter is emty, has "" value, it is not sent with the request. I didn't know that, and thought that those parameters, since they are empty, won't change enything, and with doing stuff with JSOUP I didn't pass them to the requests.

因此​​,

params.add(new BasicNameValuePair("_uqid", ""));
params.add(new BasicNameValuePair("_cdt", ""));
params.add(new BasicNameValuePair("_hsh", ""));

是感兴趣的地方。

were the places of interest.

另一件事,因为这个页面有302的响应,并JSOUP已followRedirects设置为true的默认,我不得不作出这样的错误也因为该方法是POST,而跟进要求必须是GET,但JSOUP假设它仍然POST和食堂的事情了。

Another thing, since this page has 302 response, and JSOUP has followRedirects set to "true" as default, I had to make that false also because that method is POST, and the "follow up request" has to be GET, but JSOUP assumes it's still POST and messes things up.

所以这是它,希望有人会发现这个有用:)

So that's it, hope someone will find this useful :)

这篇关于有路过的cookies问题GET请求(POST之后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆