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

走位风骚闪着腰 发布于 2015/12/24 17:11
阅读 628
收藏 0

开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>

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

加载中
0
走位风骚闪着腰
走位风骚闪着腰
没人帮忙看下? 
0
烽焱10仴
烽焱10仴
字符编码问题
走位风骚闪着腰
走位风骚闪着腰
何意?document.all.hiddenForm.action的值不一样
0
五六七
五六七
怎么我看这有乱码呀
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部