java - 处理 httpclient 中的弹出窗口

标签 java apache-httpclient-4.x

是否可以使用 apache http 客户端处理弹出窗口?

我正在尝试访问该网站。访问特定页面会弹出一个新窗口,在 http 响应中,我会得到 HTML 中的弹出新窗口代码。

有没有办法从http客户端重定向到新的弹出窗口?

我的代码

HttpResponse res = null;
HttpEntity entity = null;
DefaultHttpClient defClient = new DefaultHttpClient();

HttpPost httpost = new HttpPost("http://sudhaezine.com/svww_loginform.php?email=guest@xxx.com&password=yyy&brw=safari");
res = defClient.execute(httpost);
entity = res.getEntity();

StringBuffer sb = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String str;
while ((str = reader.readLine()) != null) {
    sb.append(new String(str.getBytes(), "UTF-8"));
}
entity.consumeContent();

这给出了以下响应

<html><head>  <title></title></head><META HTTP-EQUIV=refresh content=30;url=thankyou.php><body background="images/bg1.gif"><script language="javascript">var popupstr='';   //var bor = navigator.appName;   //alert(bor);   doPopUpWindow = window.open("svww_index1.php","StarView",'width=800,height=600,left=0,top=0'+popupstr);  // if (!doPopUpWindow) {       //Popup blocked open with in current browser window.     //  doPopUpWindow = window.open("svww_index1.php","_parent",'width='+ screen.availWidth +',height='+ screen.availHeight +',left=0,top=0'+popupstr);  // }    try{ var obj = doPopUpWindow.name;  // window.close();//WindowOpen.close();//alert("Popup blocker NOT detected");    }   catch(e){       doPopUpWindow = window.open("svww_index1.php","_parent",'width='+ screen.availWidth +',height='+ screen.availHeight +',left=0,top=0'+popupstr); }</script></body></html>

最佳答案

您无法使用 http 客户端自动处理此类弹出窗口,因为它们是通过客户端 JavaScript 代码创建的。所以你需要带有 JS 解释器的客户端(如 Web 浏览器)。

您可以继续对 JavaScript 代码进行逆向工程,将从响应正文中获取的参数传递给结果函数,并使用 http 客户端向生成的 url 发出新请求。这个过程称为Web scraping .

关于java - 处理 httpclient 中的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784567/

相关文章:

apache-httpclient-4.x - HttpClient 4.3.5的ConnectionRequestTimeOut与4.0.1中的HttpConnectionParams.setConnectionTimeout的ConnectTimeout

java - 将 2 个模式集成在一起

java - Apache HttpClient 是否批量请求?

java - 使用 Auth Token 的 HTTPGet 抛出错误代码 403

带有用于 NTLM 身份验证的 http 客户端的 Spring Rest 模板

java - HttpClient javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated when time shifted?

java.lang.NullPointerException-- -"com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)"

java - 同一类算法中的不同原始数据类型条目

java - 删除字符串数组中的重复字符串

java - 在java中使用单链表和冒泡排序进行编码