java 模拟http的get和post请求
来源:互联网 发布:网络游戏客户端编程 编辑:程序博客网 时间:2024/05/29 21:34
package com.hxkj.weatherwarn.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.MalformedURLException;import java.net.URL;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.zip.GZIPInputStream;import sun.net.www.protocol.http.HttpURLConnection;/** * http的工具类 * * HTTP协议: * 把HTTP协议理解为写信的过程; * 通过firebug,可以查看每一个链接向服务器发出的详细信息; * 请求头======向服务器写信的信封 * 响应头======服务器的回信的信封 * 参数=====向服务器写信的内容(信封里面的内容) * 响应=====服务器的回信的信封里面的内容 * @author gyq * */public class HttpUtil{/** * httpget方式请求 * 参数==发信 * @param urlStr 请求地址 * @param headerMap 信封的内容,请求头信息 * @param paramsMap 信的内容,请求的参数 * @return 返回url对应的网页内容;回信的内容,响应的内容 */public String methodGet(String urlStr,Map<String ,String> headerMap,Map<String,String> paramsMap){StringBuffer sb=new StringBuffer();/*get提交*/try{if(!urlStr.endsWith("?")){urlStr+="?";}/*get方式将参数信息传过去,并且进行拼接*/for (Iterator iterator = paramsMap.entrySet().iterator(); iterator.hasNext();){Entry entry = (Entry) iterator.next();urlStr+=entry.getKey()+"="+entry.getValue()+"&";}urlStr=urlStr.substring(0, urlStr.length()-1);ConstatFinalUtil.LOGGER.info("urlStr:{}",urlStr);/* 根据指定url字符串创建一个url对象 */URL url=new URL(urlStr);/* 查看一个urlConnection具体是什么 * 强转HttpURLConnection; 因为urlConnection本身就是HttpURLConnection;通过debug看到的 */HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();/* 如何将信封的内容设置到请求中 设置请求头信息*/for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();){Entry entry = (Entry) iterator.next();urlConnection.setRequestProperty(entry.getKey()+"", entry.getValue()+"");}/*获取输入流*///InputStream is=urlConnection.getInputStream();GZIPInputStream gzipInputStream=new GZIPInputStream(urlConnection.getInputStream());BufferedReader br=new BufferedReader(new InputStreamReader(gzipInputStream,"utf-8"));String line="";while((line=br.readLine())!=null){sb.append(line);}} catch (MalformedURLException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}return sb.toString();}/** * httppost请求 * @param urlStr * @param headerMap * @param paramsMap * @return */public String methodPost(String urlStr,Map<String ,String> headerMap,Map<String,String> paramsMap){StringBuffer sb=new StringBuffer();try{URL url=new URL(urlStr);HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();/* 发送POST请求必须设置如下两行*/urlConnection.setDoInput(true);urlConnection.setDoOutput(true);/*设置超时时间*/urlConnection.setConnectTimeout(ConstatFinalUtil.CONNECTION_TIMEOUT);urlConnection.setReadTimeout(ConstatFinalUtil.READ_TIMEOUT);/* 如何将信封的内容设置到请求中 设置请求头信息*/for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();){Entry entry = (Entry) iterator.next();urlConnection.setRequestProperty(entry.getKey()+"", entry.getValue()+"");}OutputStream os=urlConnection.getOutputStream();BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os,"utf-8"));/*输入输出流通过firebug发现post请求,发送的是以下字符串 * returnUrl=&email=22%4022.com&password=111111ddd&code=dddd*/StringBuffer paramSb=new StringBuffer();for (Iterator iterator = paramsMap.keySet().iterator(); iterator.hasNext();){String key = (String) iterator.next();/*通过key来获取键和值*/paramSb.append(key+"="+paramsMap.get(key)+"&");}/*把信息发送到服务器*/bw.write(paramSb.toString());bw.flush();bw.close();/*传参数*/GZIPInputStream gzipInputStream=new GZIPInputStream(urlConnection.getInputStream());//InputStream is=urlConnection.getInputStream();BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(gzipInputStream, "utf-8"));String line="";while((line=bufferedReader.readLine())!=null){sb.append(line);}} catch (IOException e){/*异常记录到日志文件中*/ConstatFinalUtil.LOGGER.error("发送post请求失败:Url:{},参数:{},返回:{}",urlStr,paramsMap,sb,e);}return sb.toString();}}package com.gyq.yxl.outer.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import java.util.Map;import org.junit.Test;import com.gyq.xyl.common.util.ConstatFinalUtil;import com.gyq.xyl.common.util.HttpUtil;public class SpiderTest{ @Test public void findbai() { HttpUtil httpUtil=new HttpUtil(); String urlStr=""; /*请求头*/ Map<String ,String> headerMap=new HashMap<String, String>(); headerMap.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); headerMap.put("Accept-Encoding", "gzip, deflate, br"); headerMap.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); headerMap.put("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"); /*请求参数*/ Map<String,String> paramsMap=new HashMap<String, String>(); paramsMap.put("id", "12"); paramsMap.put("name", "gyq"); String response=httpUtil.methodGet(urlStr, headerMap, paramsMap); //String response=httpUtil.methodPost(urlStr, headerMap, paramsMap); ConstatFinalUtil.LOGGER.info("响应:{}",response); } @Test public void tohtml() { HttpUtil httpUtil=new HttpUtil(); String urlStr=""; /*请求头*/ Map<String ,String> headerMap=new HashMap<String, String>(); headerMap.put("myheader","gyq"); /*headerMap.put("Accept-Encoding", "gzip, deflate, br"); headerMap.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); headerMap.put("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");*/ /*请求参数*/ Map<String,String> paramsMap=new HashMap<String, String>(); paramsMap.put("email", "645208239@qq.com"); paramsMap.put("password", "123456"); paramsMap.put("code", "0825"); System.out.println(); //String response=httpUtil.methodGet(urlStr, headerMap, paramsMap); String response=httpUtil.methodPost(urlStr, headerMap, paramsMap); ConstatFinalUtil.LOGGER.info("响应:{}",response); } /** * 测试返回json */ @Test public void testJson() { HttpUtil httpUtil=new HttpUtil(); String urlStr=""; /*请求头*/ Map<String ,String> headerMap=new HashMap<String, String>(); headerMap.put("myheader","gyq"); /* 参数 */ Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put("json", "{'ver':'v1','data':{'id':'8'}}"); String response = httpUtil.methodPost(urlStr,headerMap,paramsMap); ConstatFinalUtil.LOGGER.info("响应:{}",response); }}
0 0
- Java模拟HTTP的Get和Post请求
- Java模拟HTTP的Get和Post请求
- Java模拟HTTP的Get和Post请求
- java 模拟http的get和post请求
- Java http的get和post请求
- Java http的get和post请求
- Java利用Http 模拟POST、GET请求
- java模拟HTTP post,get请求
- Java模拟Http post、get请求
- java模拟发送http的get、post请求
- java使用HttpClient模拟发送http的get、post请求
- Android单元测试,模拟http的get和post请求
- HttpClient模拟HTTP的GET和POST请求
- Java模拟HTTP的Get和Post请求(增强) Apache-httpclient
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- PHP之文件操作,http请求数据格式,模拟get和post,CURL模拟请求的使用
- Windows7下TensorFlow安装
- 测试不同格式下depth buffer的精度
- register_shutdown_function的使用
- android MotionEvent getAction,getActionMasked,getActionIndex区别
- Vue.js学习系列(三十三)-- Vue.js样式绑定(四)
- java 模拟http的get和post请求
- ARM 笔试、面试题(摘录)__长期更新
- mac下PHPUnit安装步骤
- https证书格式转换(cer转bks)
- win7系统安装SQLServer2000的详细步骤(图文)
- www.idcnd.net传媒官方客服提供
- ios中怎么手动调label的行距
- Codeforces 710C Magic Odd Square【思维】
- SQL使用(二)-----联合查询和单查询的优缺点