登录网站以获取java中的数据
我正在尝试登录以下网站:。登录表单字段如下所示:登录网站以获取java中的数据,java,android,web-scraping,httpclient,jsoup,Java,Android,Web Scraping,Httpclient,Jsoup,我正在尝试登录以下网站:。登录表单字段如下所示: <input type="hidden" name="cookieexists" value="false"> <input size=12 type=name name=name> <input size=12 type=password name=password> <input type=submit name=subbera value="Login"> 下面是我的代码,在其中我尝试使
<input type="hidden" name="cookieexists" value="false">
<input size=12 type=name name=name>
<input size=12 type=password name=password>
<input type=submit name=subbera value="Login">
下面是我的代码,在其中我尝试使用HttpClient登录,并使用Jsoup解析生成的html。不幸的是,这将返回处于相同未登录状态的页面的原始html
HttpResponse res = null;
Document homePage = null;
HttpEntity entity = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.deeproute.com");
String html = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("cookieexists", "false"));
nameValuePairs.add(new BasicNameValuePair("name", username));
nameValuePairs.add(new BasicNameValuePair("password", pass));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
res = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
if (res != null) {
try {
html = EntityUtils.toString(res.getEntity());
homePage = Jsoup.parse(html);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpResponse res=null;
文件首页=空;
HttpEntity=null;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.deeproute.com");
字符串html=null;
List nameValuePairs=新的ArrayList(3);
添加(新的BasicNameValuePair(“cookieexists”、“false”);
添加(新的BasicNameValuePair(“名称”,用户名));
添加(新的BasicNameValuePair(“密码”,pass));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
res=httpclient.execute(httppost);
}捕获(IOE异常){
e、 printStackTrace();
}
如果(res!=null){
试一试{
html=EntityUtils.toString(res.getEntity());
homePage=Jsoup.parse(html);
}捕获(解析异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
如何解决此问题?仅使用jSoup代码的工作解决方案
- 步骤1.获取登录表单
- 步骤2.张贴附有cookies和所有参数的表单
仅使用jSoup代码的工作解决方案
- 步骤1.获取登录表单
- 步骤2.张贴附有cookies和所有参数的表单
public static void main(String[] args) throws Exception {
Connection.Response loginForm = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
.method(Connection.Method.GET)
.execute();
Document document = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
.data("cookieexists", "false")
.data("name", "username")
.data("password", "pass")
.data("subbera", "Login")
.cookies(loginForm.cookies())
.post();
}