私は数日間この問題に取り残されています。異なる組み合わせを試してみましたが、成功していない時から目が痛いです。物事は、私はインターネットからデータを取得し、それを解析し、それをユーザーに示す必要があるアプリケーションを作っている。私はそれを行うためにいくつかの方法を試しました。そして、JSOUPを使うことは非常に役に立ちました。特に、解析して結果からデータを取り出すことができました。GETリクエスト(POST後)にクッキーを渡す際の問題
しかし、解決できない問題が1つあります。私は通常の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();
}
ので、最終的な結果が返されたデータを含む文字列になります。しかし、私が何をしようと、私は "空の"ページを取得し、それは解析されたテキストであり、私はブラウザで要求されるすべてをシミュレートしました。私はこの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
、(その応答)、データ:ここ
は、POSTがあると、ブラウザで撮影した生のヘッダをGET必要なものはありますが、パラメータ、クッキー、または私が試したすべてのものを組み合わせて、私がPOSTをしてそのデータが必要なことを「考える」ことができませんでした。
ここにJSOUPパーサーのないバージョンのコードがありますが、これらのクッキーをチェックしても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<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エンティティ、私のセットアップのエラーを見つけるか、私を見つけるこれらの2つの要求を作るための方法をして、データを取得することができれば、素敵なJSOUPパーサーに、それは素晴らしいだろう。あるいは、私はこのページに必要なものについて何かを得て、別のパラメータでリクエストをする必要があると思います。私はWiresharkとCharles Debugging Proxyを使用して何を作成するのかを試してみました(両方を試してみてください)、そのセッションID、fe_typo_user、サイトの時間を追跡するための他のパラメータなどが見つかりました。それらも渡します。 "_ utma" " _utmb" ...など。
私はいくつかの方法を使っていますが、「より単純な」POSTメソッドだけではデータが返されていますが、成功しましたが、このサイトのこの特定の問題は私を狂わせてしまいます。あなたの助けを前にありがとう。