Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with passing cookies to GET request (after POST)
    primarykey
    data
    text
    <p>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.</p> <p>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):</p> <pre><code>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&amp;") .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(); } </code></pre> <p>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.</p> <p>Here are the POST and GET raw headers captured by the browser: (post)</p> <pre><code>&gt; POST /imenik_telefon.html?a=search HTTP/1.1 Host: www.bhtelecom.ba &gt; Content-Length: 56 Cache-Control: max-age=0 Origin: &gt; http://www.bhtelecom.ba User-Agent: Mozilla/5.0 (Windows NT 6.1; &gt; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 &gt; Safari/535.1 Content-Type: application/x-www-form-urlencoded Accept: &gt; text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 &gt; Referer: http://www.bhtelecom.ba/index.php?id=3226&amp; Accept-Encoding: &gt; gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: &gt; ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: &gt; PHPSESSID=opavncj3317uidbt93t9bie980; &gt; fe_typo_user=332a76d0b1d4944bdbbcd28d63d62d75; &gt; __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) &gt; &gt; di=033&amp;br=123456&amp;_uqid=&amp;_cdt=&amp;_hsh=&amp;btnSearch=Tra%C5%BEi </code></pre> <p>(get)</p> <pre><code>&gt; GET /index.php?id=3226&amp; HTTP/1.1 Host: www.bhtelecom.ba Cache-Control: &gt; max-age=0 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) &gt; AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1 &gt; Accept: &gt; text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 &gt; Referer: http://www.bhtelecom.ba/index.php?id=3226&amp; Accept-Encoding: &gt; gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: &gt; ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: &gt; PHPSESSID=opavncj3317uidbt93t9bie980; &gt; __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 </code></pre> <p>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.</p> <p>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.</p> <pre><code>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&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); 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&lt;Cookie&gt; cookies = client.getCookieStore().getCookies(); if (cookies.isEmpty()) { Log.d(TAG, "no cookies"); } else { for (int i = 0; i &lt; 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&amp;"); get.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); HttpResponse responseGET = client.execute(get); HttpEntity entityGET = responseGET.getEntity(); List&lt;Cookie&gt; cookiesGet = client.getCookieStore().getCookies(); if (cookies.isEmpty()) { Log.d(TAG, "no cookies"); } else { for (int i = 0; i &lt; 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(); } </code></pre> <p>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, "_<em>utma" "</em>_utmb" ... and so on.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload