Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
登录ASP.NET Web表单的Java方法_Java_Screen Scraping_Httpurlconnection - Fatal编程技术网

登录ASP.NET Web表单的Java方法

登录ASP.NET Web表单的Java方法,java,screen-scraping,httpurlconnection,Java,Screen Scraping,Httpurlconnection,我正在开发一个java程序,该程序需要登录到ASP.NET web表单,然后在经过身份验证后下载一个文件。正常的HTTP GET/POST不是问题,但当我从java连接时,ASP似乎没有给我会话ID,而是从浏览器 当我查看Firefox中的标题信息时,我看到cookies是从最初登录时设置的,但随后页面会立即重定向到新的URL。我不确定这是否重要,但登录后重定向到的页面包含iFrame。我已经尝试在其中加载主页和iframe src,但都没有在标题中提供cookie //Pull up the

我正在开发一个java程序,该程序需要登录到ASP.NET web表单,然后在经过身份验证后下载一个文件。正常的HTTP GET/POST不是问题,但当我从java连接时,ASP似乎没有给我会话ID,而是从浏览器

当我查看Firefox中的标题信息时,我看到cookies是从最初登录时设置的,但随后页面会立即重定向到新的URL。我不确定这是否重要,但登录后重定向到的页面包含iFrame。我已经尝试在其中加载主页和iframe src,但都没有在标题中提供cookie

//Pull up the login page, extract out the hidden input variables __VIEWSTATE, __EVENTVALIDATION
URL url = new URL(loginPage);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
//This reads the page line-by-line and extracts out all the values from hidden input fields
Map<String,String> formFields = getViewstate(conn);

//Now re-open the URL to actually submit the POST data
conn = (HttpURLConnection) url.openConnection();            
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String postValues = URLEncoder.encode("txtUsername", "UTF-8") + "=" + URLEncoder.encode(uid, "UTF-8");
postValues += "&" + URLEncoder.encode("txtPassword", "UTF-8") + "=" + URLEncoder.encode(pwd, "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTTARGET", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
postValues += "&" + URLEncoder.encode("__VIEWSTATE", "UTF-8") + "=" + URLEncoder.encode(formFields.get("viewstate"), "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTVALIDATION", "UTF-8") + "=" + URLEncoder.encode(formFields.get("eventvalidation"), "UTF-8");
out.writeBytes(postValues);
out.flush();
out.close();
//At this point looking at Firefox sniffer data, it should be sending back the cookie
//However there is no Set-Cookie in the header fields
for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
        // get ASP.NET_SessionId from cookie
    if (key.equalsIgnoreCase("set-cookie")) {
        sessionId = conn.getHeaderField(key);
        sessionId = sessionId.substring(0, sessionId.indexOf(";"));
    }
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
    //The page it prints out is the page it was redirected to when logged in through the browser
    System.out.println(line);
}
rd.close();
//At this point, it was a successful login, but I never got the cookie so I'm stuck
//打开登录页面,提取隐藏的输入变量_VIEWSTATE,_EVENTVALIDATION
URL=新URL(登录页面);
HttpURLConnection conn=null;
conn=(HttpURLConnection)url.openConnection();
//这将逐行读取页面,并从隐藏的输入字段中提取所有值
Map formFields=getViewstate(conn);
//现在重新打开URL以实际提交帖子数据
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
连接设置输出(真);
conn.setDoInput(真);
DataOutputStream out=新的DataOutputStream(conn.getOutputStream());
字符串postValues=URLEncoder.encode(“txtUsername”,“UTF-8”)+“=”+URLEncoder.encode(uid,“UTF-8”);
postValues+=“&”+urlcoder.encode(“txtPassword”,“UTF-8”)+“=”+urlcoder.encode(pwd,“UTF-8”);
postValues+=“&”+URLEncoder.encode(“\uu事件目标”,“UTF-8”)+“=”+URLEncoder.encode(“,“UTF-8”);
postValues+=“&”+URLEncoder.encode(“\uuu VIEWSTATE”,“UTF-8”)+“=”+URLEncoder.encode(formFields.get(“VIEWSTATE”),“UTF-8”);
postValues+=“&”+URLEncoder.encode(“\uuu事件验证”,“UTF-8”)+“=”+URLEncoder.encode(formFields.get(“事件验证”),“UTF-8”);
out.writeBytes(postValue);
out.flush();
out.close();
//现在查看Firefox嗅探器数据时,它应该会发回cookie
//但是,标题字段中没有设置Cookie
for(int i=1;(key=conn.getHeaderFieldKey(i))!=null;i++){
//从cookie获取ASP.NET_会话ID
if(key.equalsIgnoreCase(“设置cookie”)){
sessionId=conn.getHeaderField(键);
sessionId=sessionId.substring(0,sessionId.indexOf(“;”);
}
}
BufferedReader rd=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
而((line=rd.readLine())!=null){
//它打印出来的页面是通过浏览器登录时重定向到的页面
系统输出打印项次(行);
}
rd.close();
//在这一点上,这是一个成功的登录,但我从来没有得到饼干,所以我卡住了

您试图访问的站点似乎依赖于HttpURLConnection不支持的Cookie。解决这个问题的一种方法是使用类似于模拟浏览器的库(支持cookie、javascript等)。

HttpClient,我相信HtmlUnit是基于它的,它具有我认为您需要的较低级别的功能。很好地处理cookies,但是如果您需要更多cookies,那么Kurt的观点是正确的,您应该寻找具有更多功能的cookies。如果您确实需要获得完整的浏览器功能,您可以尝试类似于Selenium/Webdriver的东西,它可以在编程控制下实现浏览器的自动化

我应该能够解析出标题来提取cookies,对吗?我得到了所有的标准头返回,但没有饼干。除非我需要明确地告诉服务器我接受cookies,最后一个是一个好的观点,否则我认为大多数服务器都有一些机制来测试是否支持Cookie。我知道Weblogic会在第一个请求的URL和cookie中设置会话id,然后在下一个请求中,它会将URL值与预期的cookie进行比较,如果它们匹配,则会继续删除URL编码的id。如果你拿到了饼干,你会把它送回去吗?是否已将跟随重定向设置为true?一个更高级别的库可能仍然是您在没有大量自定义代码的情况下实现所需功能的最佳选择。