使用HttpComponents访问获取不到想要的数据,什么原因?

发布于 2021-11-25 23:16:23 字数 5731 浏览 785 评论 4

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;
	}
}


代码zip包:http://pan.baidu.com/s/1boq4Ivl

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把回忆走一遍 2021-12-01 04:36:09

怎么我看这有乱码呀

终止放荡 2021-12-01 03:32:19

何意?document.all.hiddenForm.action的值不一样

顾忌 2021-11-29 22:48:19

字符编码问题

百思不得你姐 2021-11-29 09:10:18

没人帮忙看下? 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文