使用HttpComponents访问获取不到想要的数据,什么原因?
http://fpcx.tjcs.gov.cn/TJTAX_NET/NetLevy/NetQuery/TicketUse/index.jsp
当访问提交到http://fpcx.tjcs.gov.cn/servlet/com.appinf.bus.TaskBus?_TaskID=tjtax.declevy.NetQuery.TicketQuery.si.C_UseTicketQuerySI_siTicketQuery&_ReturnPage=/TJTAX_NET/NetLevy/NetQuery/TicketUse/edit.jsp&_ReturnArea=editArea,返回的信息和手工的不一致,啥原因?
手工输出:
<html> <head><title>�:</title></head> <body> <form name='hiddenForm' method='post'></form> <script language="javascript"> document.all.hiddenForm.action = "/TJTAX_NET/NetLevy/NetQuery/TicketUse/edit.jsp?_SessionName=/TJTAX_NET/NetLevy/NetQuery/TicketUse/index.jsp&_ReturnArea=editArea&_ReturnPage=/TJTAX_NET/NetLevy/NetQuery/TicketUse/edit.jsp&_FIELDS=null"; document.all.hiddenForm.target = "editArea"; document.all.hiddenForm.submit(); </script> </body></html>
程序输出:
<html> <head><title>?:</title></head> <body> <form name='hiddenForm' method='post'></form> <script language="javascript"> document.all.hiddenForm.action = "/view/share/error/errorResult.jsp?code=E070016"; document.all.hiddenForm.target = "editArea"; document.all.hiddenForm.submit(); </script> </body></html>
package test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpHeaders; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * 测试天津地税Http请求 */ public class TJ { private String welUrl = "http://fpcx.tjcs.gov.cn/TJTAX_NET/NetLevy/NetQuery/TicketUse/index.jsp"; private String valiUrl = "http://fpcx.tjcs.gov.cn/servlet/com.appinf.bus.TaskBus?_TaskID=tjtax.declevy.NetQuery.TicketQuery.si.C_UseTicketQuerySI_siTicketQuery&_ReturnPage=/TJTAX_NET/NetLevy/NetQuery/TicketUse/edit.jsp&_ReturnArea=editArea"; public static void main(String[] args) { TJ tj = new TJ(); System.out.println(tj.visit()); } public String visit() { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpClientContext context = HttpClientContext.create(); HttpGet welGet = new HttpGet(welUrl); HttpPost valiPost = new HttpPost(valiUrl); valiPost.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); valiPost.addHeader(HttpHeaders.REFERER, "http://fpcx.tjcs.gov.cn/TJTAX_NET/NetLevy/NetQuery/TicketUse/edit.jsp?_SessionName=/TJTAX_NET/NetLevy/NetQuery/TicketUse/index.jsp&_ReturnArea=editArea&_ReturnPage=/TJTAX_NET/NetLevy/NetQuery/TicketUse/edit.jsp&_FIELDS=null"); valiPost.addHeader(HttpHeaders.CONNECTION, "keep-alive"); valiPost.addHeader(HttpHeaders.HOST, "fpcx.tjcs.gov.cn"); String resp = ""; try { httpClient.execute(welGet, context); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("_SIVO", "tjtax.declevy.NetQuery.TicketQuery.vo.C_UseTicketQueryVO")); nvps.add(new BasicNameValuePair("_SessionName", "/TJTAX_NET/NetLevy/NetQuery/TicketUse/index.jsp")); nvps.add(new BasicNameValuePair("cxfs", "1")); nvps.add(new BasicNameValuePair("S_INVOICECODE", "")); nvps.add(new BasicNameValuePair("S_TICKETCODE", "")); nvps.add(new BasicNameValuePair("S_TICKETCHECKCODE", "212001504022038636503029")); nvps.add(new BasicNameValuePair("S_KPJE", "1187")); nvps.add(new BasicNameValuePair("CALS_YEARNUMGB", "")); nvps.add(new BasicNameValuePair("CBI_TICKETCODE", "")); nvps.add(new BasicNameValuePair("BTO_SUBID", "")); nvps.add(new BasicNameValuePair("CBI_INVOICENAME", "")); nvps.add(new BasicNameValuePair("KPRQ", "")); nvps.add(new BasicNameValuePair("KPJE", "")); nvps.add(new BasicNameValuePair("CFP_USEIGN", "")); nvps.add(new BasicNameValuePair("SQFPDWMC", "")); nvps.add(new BasicNameValuePair("PRJ_BTOSUBID", "")); nvps.add(new BasicNameValuePair("KFPDWMC", "")); UrlEncodedFormEntity url = new UrlEncodedFormEntity(nvps, "UTF-8"); valiPost.setEntity(url); System.out.println("携带cookie:" + context.getCookieStore()); CloseableHttpResponse valiResp = httpClient.execute(valiPost, context); resp = EntityUtils.toString(valiResp.getEntity()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { welGet.abort(); valiPost.abort(); try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return resp; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么我看这有乱码呀
何意?document.all.hiddenForm.action的值不一样
字符编码问题
没人帮忙看下?