将Cookie传递给GET请求的问题(POST后)

我现在被困在这个问题好几天了,我的眼睛开始受伤于尝试不同组合的时间,但没有成功。 问题是,我正在制作一个应用程序,它必须从互联网上获取数据,解析它然后显示给用户。 我已经尝试了几种方法,并且使用JSOUP非常有帮助,特别是在解析和从结果中获取数据时。

但是,有一个问题我无法解决。 我已尝试使用常规HTTPClient和JSOUP,但我无法成功获取所需的数据。 这是我的代码(JSOUP版本):

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

因此,最终结果将是包含返回数据的字符串。 但是,无论我尝试什么,我都会得到“空白”页面,它是解析后的文本,我已经模拟了浏览器请求的所有内容。

以下是浏览器捕获的POST和GET原始标头:(发布)

 > 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 

在这个GET,(它的响应),我需要的数据,但任何参数,cookie,或我尝试的一切的组合,我无法让它“认为”我做了一个POST,现在想要这些数据。

这是没有JSOUP解析器的我的代码版本,但是我也无法让它工作,虽然当我检查这些cookie时,它们没问题,POST和GET也一样,但没有成功。

 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 params = new ArrayList(); 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 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 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实体用作可爱的JSOUP解析器的输入(InputStream),太棒了 或者也许我得到了关于页面需要什么的全部内容,我需要用不同的参数提出我的请求,我将不胜感激。 我使用Wireshark和Charles Debugging Proxy来了解要创建的内容(尝试两者,仔细检查),并且只找到会话ID,fe_typo_user和其他一些用于跟踪网站上的时间等参数,我试过了传递它们,“_ utma” “_ utmb ”……等等。

我有一些其他的方法,使用“更简单”,仅POST方法与数据作为响应,我已经成功地得到了,但这个网站的这个特定问题让我发疯。 在此先感谢您的帮助。

经过许多小时的尝试和跟踪传入/传出数据包后,我终于找到了解决方案。

事情是“bug”,或HTTPClient的行为。 如果向post添加参数,并且参数为emty,则具有“”值,则不会随请求一起发送。 我不知道这一点,并认为那些参数,因为它们是空的,不会改变enything,并且使用JSOUP做的事情我没有将它们传递给请求。

所以,

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

是感兴趣的地方。

另一件事,因为这个页面有302响应,并且JSOUP将followRedirects设置为“true”作为默认值,我不得不将其设为假,因为该方法是POST,并且“跟进请求”必须是GET,但JSOUP假设它仍然是POST并且搞砸了。

所以就是这样,希望有人会觉得这很有用:)