我已经在这个问题上停留了好几天了,我的眼睛开始因为花时间尝试不同的组合而受伤,但没有成功。问题是,我正在制作一个应用程序,它必须从互联网上获取数据,解析它,然后将其显示给用户。我已经尝试了几种方法来做到这一点,使用 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 原始 header : (发布)
> 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<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 实体,然后我可以将其用作可爱的 JSOUP 解析器的输入(InputStream) , 这将是惊人的。或者,也许我了解了关于页面需要什么的全部内容,并且我需要使用不同的参数发出请求,我将不胜感激。我使用 Wireshark 和 Charles Debugging Proxy 来了解要创建的内容(都尝试过,仔细检查),并且只发现 session ID、fe_typo_user 和其他一些用于跟踪现场时间等的参数,我已经尝试过也传递它们,“_utma”“_utmb”......等等。
我有一些其他方法,使用“更简单”的仅 POST 方法和响应数据,我已经成功地做到了,但是这个网站的这个具体问题让我发疯。预先感谢您的帮助。
最佳答案
经过许多小时的尝试并跟踪传入/传出数据包后,我终于找到了解决方案。
问题出在“错误”或 HTTPClient 的行为上。如果您向帖子添加参数,并且参数为空,具有“”值,则不会随请求一起发送。我不知道,并且认为那些参数,因为它们是空的,不会改变任何东西,并且在使用 JSOUP 做一些事情时,我没有将它们传递给请求。
所以,
params.add(new BasicNameValuePair("_uqid", ""));
params.add(new BasicNameValuePair("_cdt", ""));
params.add(new BasicNameValuePair("_hsh", ""));
是名胜古迹。
另一件事,因为这个页面有 302 响应,并且 JSOUP 默认将 followRedirects 设置为“true”,我不得不将其设置为 false,因为该方法是 POST,而“跟进请求”必须是 GET,但是 JSOUP 假设它仍然是 POST 并且把事情搞砸了。
就是这样,希望有人会觉得这有用:)
关于java - 将 cookie 传递给 GET 请求的问题(POST 之后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7897524/