标签:import
使用浏览器工具查看请求url及请求头
package com.httpclient; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import java.util.Scanner; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class Main3 { static Random random = null; public int getBirthYear() { //生成随机年份 int result = (int) (1975 + Math.random() * 15); return result; } public int getBirthMonth() { //生成随机月份 int result = (int) (1 + Math.random() * 12); return result; } public int getBirthDay() { //生成随机日期 int result = (int) (1 + Math.random() * 28); //random = null; return result; } public static void main(String[] args) { random = new Random(System.currentTimeMillis()); Main3 main3 = new Main3(); String _txtName = ""; Scanner scanner = new Scanner(System.in); String _dtcBirthdate = main3.getBirthYear() + "-" + main3.getBirthMonth() + "-" + main3.getBirthDay(); String _dtcReplyTime = "2015-" + "4-" + main3.getBirthDay(); String _txtWhyfor = "什么程度构成犯罪"; String _txtReplyOpinion = "轻伤以上"; for (int i = 0; i < 10; i++) { random = new Random(System.currentTimeMillis()); _dtcBirthdate = main3.getBirthYear() + "-" + main3.getBirthMonth() + "-" + main3.getBirthDay(); _dtcReplyTime = "2015-" + "4-" + main3.getBirthDay(); _txtName = scanner.next(); System.out.print(_dtcBirthdate + " "); System.out.println(_dtcReplyTime); post(_txtName, "1", _dtcBirthdate, "B0300", "2", _dtcReplyTime, _txtWhyfor, _txtReplyOpinion); } } //public static void main(String[] args ) { public static void post(String _txtName ,String _ddlSex,String _dtcBirthdate,String _txt_caseApplyItemTypeCode ,String _ddl_legalItemType,String _dtcReplyTime,String _txtWhyfor,String _txtReplyOpinion){ String url = " HttpPost httpPost = new HttpPost(url);// 创建HttpPost对象 List<NameValuePair> params = new ArrayList<NameValuePair>(); String __VIEWSTATE = "/wEPDwUKLTk5NTEwODc4Mg9kFgICAw9kFiYC";//从浏览器中获取 params.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE)); /* String _txtName = "杜芳芳"; String _ddlSex = "2";//1男 2女 String _dtcBirthdate = "1986-5-4"; String _txt_caseApplyItemTypeCode = "M0101";//eg.离婚纠纷 String _ddl_legalItemType = "1";//咨询事项类型 民事1 刑事2 行政3 String _dtcReplyTime = "2015-4-1";//接受日期 String _txtWhyfor = "离婚的程序";//咨询内容 String _txtReplyOpinion = "可双方协商到民政局登记也可到法院起诉离婚";//答复意见 */ String txtName = "余伟";// 姓 名 params.add(new BasicNameValuePair("txtName", _txtName)); String ddlSex = "1";// 性别 params.add(new BasicNameValuePair("ddlSex", _ddlSex)); /* * <option value="1"> 男</option> <option value="2"> 女</option> <option * value="3"> 未说明</option> */ String dtcBirthdate = "1987-03-4";// 出生时间 params.add(new BasicNameValuePair("dtcBirthdate", _dtcBirthdate)); String ddlCountry = "中国";// 国籍 params.add(new BasicNameValuePair("ddlCountry", ddlCountry)); String ddlNation = "汉族"; params.add(new BasicNameValuePair("ddlNation", ddlNation)); String ddlHealth = "10";// 健康状况 params.add(new BasicNameValuePair("ddlHealth", ddlHealth)); String ddlAdvisoryHumanCount = "1";// 同来人数 params.add(new BasicNameValuePair("ddlAdvisoryHumanCount", ddlAdvisoryHumanCount)); String txt_familyRegion = "湖北省黄冈市麻城市";// 所住地区 湖北省黄冈市麻城市 params.add(new BasicNameValuePair("txt_familyRegion", txt_familyRegion)); String txt_familyRegionCode = "421181";// 所住地区代码 421181 params.add(new BasicNameValuePair("txt_familyRegionCode", txt_familyRegionCode)); String dl_humanCrowType = "00005";// 受援人类别 农民 00005 params.add(new BasicNameValuePair("dl_humanCrowType$ctl06$chkItem", dl_humanCrowType)); String ddl_legalItemType = "1";// 咨询事项类型 params.add(new BasicNameValuePair("ddl_legalItemType", _ddl_legalItemType)); /* * <option value="1" trimedText="民事"> 民事</option> <option value="2" * trimedText="刑事"> 刑事</option> <option value="3" * trimedText="行政"> 行政</option> */ String txt_caseApplyItemType1 = "故意伤害罪"; params.add(new BasicNameValuePair("txt_caseApplyItemType1", txt_caseApplyItemType1)); String txt_caseApplyItemTypeShow = "故意伤害罪"; params.add(new BasicNameValuePair("txt_caseApplyItemTypeShow", txt_caseApplyItemTypeShow)); String txt_caseApplyItemTypeCode = "M0101";//M0101",fname:" 离婚纠纷 params.add(new BasicNameValuePair("txt_caseApplyItemTypeCode", _txt_caseApplyItemTypeCode)); String ProcessMode = "1";// 处理方式 params.add(new BasicNameValuePair("ddlProcessMode", ProcessMode)); /* * <option value="1"> 咨询</option> <option value="3"> 代书</option> <option * value="211"> 引导向其他渠道求助</option> <option * value="212"> 在咨询中申请法援(未受理)</option> <option * value="213"> 在咨询中申请法援(受理并批准)</option> */ String dtcReplyTime = "2015-04-15";// 接待日期 params.add(new BasicNameValuePair("dtcReplyTime", _dtcReplyTime)); String txtReplyer = "**";// 接待人 params.add(new BasicNameValuePair("txtReplyer", txtReplyer)); String txtWhyfor = "工伤事故损害赔偿纠纷";// 咨询内容 params.add(new BasicNameValuePair("txtWhyfor", _txtWhyfor)); String txtReplyOpinion = "确认劳动关系,提供伤残鉴定及劳动能力鉴定";// 答复意见 params.add(new BasicNameValuePair("txtReplyOpinion", _txtReplyOpinion)); String btn_Save = "保存"; params.add(new BasicNameValuePair("btn_Save", btn_Save)); String Host = "59.175.148.*"; String UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"; httpPost.setHeader("UserAgent", UserAgent); String Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; httpPost.setHeader("Accept", Accept); String AcceptLanguage = "ru,uz;q=0.8,en-US;q=0.7,zh-CN;q=0.5,zh;q=0.3,en;q=0.2"; httpPost.setHeader("AcceptLanguage", AcceptLanguage); String AcceptEncoding = "gzip, deflate"; httpPost.setHeader("AcceptEncoding", AcceptEncoding); //请求头从浏览器中获取 String Cookie = "ASP.NET_SessionId=fzozxwq2zke25ff3yxx3x3f; CheckCode=3xkgn; LegalAidV2_JSuserId=%c3%8c; LegalAidV2_JSuserName=%c3%8f%c2%af%c3%8e%c2%ac%c3%8b%c2%ad%c3%88%c2%91%c3%83%c2%92%c3%84%c2%97%c3%8b%c2%96%c3%80%c2%91%c3%9e; LegalAidV2_JSuserChinaName=%e5%bd%b5%e5%ba%93; LegalAidV2_JSdeptCode=%c3%8f; LegalAidV2_JSdeptName=%e6%ba%a8%e5%8e%8c%e7%9f%bd%e6%b1%88%e5%bd%b1%e6%8d%ab%e5%89%91%e4%ba%8c%e5%bc%b5; LegalAidV2_JSisDeveloper=%c3%8e; LegalAidV2_JSdutyUserId=%c3%8c; LegalAidV2_JSdutyUserName=%c3%8f%c2%af%c3%8e%c2%ac%c3%8b%c2%ad%c3%88%c2%91%c3%83%c2%92%c3%84%c2%97%c3%8b%c2%96%c3%80%c2%91%c3%9e; LegalAidV2_JSencrypted=%c3%8e; LegalAidV2_JSremainLogin=%c3%8e; lawyerId=%c3%8f%c2%af%c3%8e%c2%ac%c3%8b%c2%ad%c3%88%c2%91%c3%83%c2%92%c3%84%c2%97%c3%8b%c2%96%c3%80%c2%91%c3%9e"; httpPost.setHeader("Cookie", Cookie); String Connection = "keep-alive"; httpPost.setHeader("Connection", Connection); String CacheControl = "max-age=0"; httpPost.setHeader("CacheControl", CacheControl); String result = ""; try { httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); if(httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = httpResponse.getEntity(); result = EntityUtils.toString(httpEntity);//取出应答字符串 } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } System.out.println(result); } }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1643605
标签:import
原文地址:http://shamrock.blog.51cto.com/2079212/1643605