如何用java java模拟发送get请求 HTTP数据包,该如何处理

package com.jd.vd.manage.util.
import java.io.BufferedRimport java.io.Fimport java.io.IOEimport java.io.InputSimport java.io.InputStreamRimport java.nio.charset.C
import org.apache.http.HttpRimport org.apache.http.client.ClientProtocolEimport org.apache.http.client.methods.HttpPimport org.apache.http.entity.mime.HttpMultipartMimport org.apache.http.entity.mime.MultipartEimport org.apache.http.entity.mime.content.FileBimport org.apache.http.entity.mime.content.StringBimport org.apache.http.impl.client.DefaultHttpC
public class Test {
public static void main(String[] args) throws ClientProtocolException, IOException {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,"--------------------HV2ymHFg03ehbqgZCaKO6jyH", Charset.defaultCharset());
multipartEntity.addPart("ram",new StringBody("4654646", Charset.forName("UTF-8")));
multipartEntity.addPart("key",new StringBody("123456",Charset.forName("UTF-8")));
multipartEntity.addPart("from",new StringBody("cw",Charset.forName("UTF-8")));
multipartEntity.addPart("file",new FileBody(new File("D:/upload/JDELOG.txt")));
HttpPost request = new HttpPost("http://localhost:8080/sdkServlet");
request.setEntity(multipartEntity);
request.addHeader("Content-Type","multipart/form- boundary=--------------------HV2ymHFg03ehbqgZCaKO6jyH");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response =httpClient.execute(request);
InputStream is = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
System.out.println("发送消息收到的返回:"+buffer.toString());
阅读(...) 评论()用户名:umgsai
文章数:386
评论数:70
访问量:117310
注册日期:
阅读量:1297
阅读量:3317
阅读量:458205
阅读量:1142878
51CTO推荐博文
使用浏览器工具查看请求url及请求头package&com.
import&java.util.ArrayL
import&java.util.D
import&java.util.L
import&java.util.R
import&java.util.S
import&org.apache.http.HttpE
import&org.apache.http.HttpR
import&org.apache.http.NameValueP
import&org.apache.http.client.entity.UrlEncodedFormE
import&org.apache.http.client.methods.HttpP
import&org.apache.http.impl.client.DefaultHttpC
import&org.apache.http.message.BasicNameValueP
import&org.apache.http.protocol.HTTP;
import&org.apache.http.util.EntityU
public&class&Main3&{
&static&Random&random&=&
&public&int&getBirthYear()&{
&//生成随机年份
&&int&result&=&(int)&(1975&+&Math.random()&*&15);
&public&int&getBirthMonth()&{
&//生成随机月份
&&int&result&=&(int)&(1&+&Math.random()&*&12);
&public&int&getBirthDay()&{
&//生成随机日期
&&int&result&=&(int)&(1&+&Math.random()&*&28);
&&//random&=&
&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&=&"";
&&String&_txt_caseApplyItemTypeCode&=&"M0101";//eg.离婚纠纷
&&String&_ddl_legalItemType&=&"1";//咨询事项类型&民事1&&刑事2&行政3
&&String&_dtcReplyTime&=&"";//接受日期
&&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&=&"";//&出生时间
&&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&=&"";//&接待日期
&&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/&Firefox/37.0";
&&httpPost.setHeader("UserAgent",&UserAgent);
&&String&Accept&=&"text/html,application/xhtml+xml,application/q=0.9,*/*;q=0.8";
&&httpPost.setHeader("Accept",&Accept);
&&String&AcceptLanguage&=&"ru,q=0.8,en-US;q=0.7,zh-CN;q=0.5,q=0.3,q=0.2";
&&httpPost.setHeader("AcceptLanguage",&AcceptLanguage);
&&String&AcceptEncoding&=&"gzip,&deflate";
&&httpPost.setHeader("AcceptEncoding",&AcceptEncoding);
&&//请求头从浏览器中获取
&&String&Cookie&=&"ASP.NET_SessionId=fzozxwq2zke25ff3yxx3x3f;&CheckCode=3&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&=&"";
&&&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);
}&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)Java模拟POST表单提交HttpClient操作 - 开发者博客 - ITeye博客
public static void Login() {
String url = "http://www.***.com/login";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = {
new NameValuePair("account", "yijianfeng_"),
new NameValuePair("nextUrl", ""),
new NameValuePair("lcallback", ""),
new NameValuePair("password
", "******"),
new NameValuePair("persistent", "1"), };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = 0;
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location =
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("diandianLogin:" + location);
System.err.println("Location field value is null.");
System.out.println(postMethod.getStatusLine());
String str = "";
str = postMethod.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
System.out.println(str);
postMethod.releaseConnection();
其中需要的jar包:
1、 commons-httpclient.jar
2、commons-codec.jar
3、commons-logging.jar
开发者博客:
浏览 16468
浏览: 168544 次
来自: 深圳}

我要回帖

更多关于 java 解析udp数据包 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信