java模拟http_java模拟http的get和post请求

importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.io.Reader;importjava.net.HttpURLConnection;importjava.net.ProtocolException;importjava.net.URL;importjava.util.Map;/*** HTTP工具

*@authorrobinzhang

**/

public classHttpUtil {/*** 请求类型: GET*/

public final static String GET = "GET";/*** 请求类型: POST*/

public final static String POST = "POST";/*** 模拟Http Get请求

*@paramurlStr

* 请求路径

*@paramparamMap

* 请求参数

*@return*@throwsException*/

public static String get(String urlStr, Map paramMap) throwsException{

urlStr= urlStr + "?" +getParamString(paramMap);

HttpURLConnection conn= null;try{//创建URL对象

URL url = newURL(urlStr);//获取URL连接

conn =(HttpURLConnection) url.openConnection();//设置通用的请求属性

setHttpUrlConnection(conn, GET);//建立实际的连接

conn.connect();//获取响应的内容

returnreadResponseContent(conn.getInputStream());

}finally{if(null!=conn) conn.disconnect();

}

}/*** 模拟Http Post请求

*@paramurlStr

* 请求路径

*@paramparamMap

* 请求参数

*@return*@throwsException*/

public static String post(String urlStr, Map paramMap) throwsException{

HttpURLConnection conn= null;

PrintWriter writer= null;try{//创建URL对象

URL url = newURL(urlStr);//获取请求参数

String param =getParamString(paramMap);//获取URL连接

conn =(HttpURLConnection) url.openConnection();//设置通用请求属性

setHttpUrlConnection(conn, POST);//建立实际的连接

conn.connect();//将请求参数写入请求字符流中

writer = newPrintWriter(conn.getOutputStream());

writer.print(param);

writer.flush();//读取响应的内容

returnreadResponseContent(conn.getInputStream());

}finally{if(null!=conn) conn.disconnect();if(null!=writer) writer.close();

}

}/*** 读取响应字节流并将之转为字符串

*@paramin

* 要读取的字节流

*@return*@throwsIOException*/

private static String readResponseContent(InputStream in) throwsIOException{

Reader reader= null;

StringBuilder content= newStringBuilder();try{

reader= newInputStreamReader(in);char[] buffer = new char[1024];int head = 0;while( (head=reader.read(buffer))>0){

content.append(new String(buffer, 0, head));

}returncontent.toString();

}finally{if(null!=in) in.close();if(null!=reader) reader.close();

}

}/*** 设置Http连接属性

*@paramconn

* http连接

*@return*@throwsProtocolException

*@throwsException*/

private static void setHttpUrlConnection(HttpURLConnection conn, String requestMethod) throwsProtocolException{

conn.setRequestMethod(requestMethod);

conn.setRequestProperty("accept", "*/*");

conn.setRequestProperty("Accept-Language", "zh-CN");

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

conn.setRequestProperty("Proxy-Connection", "Keep-Alive");if(null!=requestMethod &&POST.equals(requestMethod)){

conn.setDoOutput(true);

conn.setDoInput(true);

}

}/*** 将参数转为路径字符串

*@paramparams

* 参数

*@return

*/

private static String getParamString(MapparamMap){if(null==paramMap ||paramMap.isEmpty()){return "";

}

StringBuilder builder= newStringBuilder();for(String key : paramMap.keySet() ){

builder.append("&")

.append(key).append("=").append(paramMap.get(key));

}return builder.deleteCharAt(0).toString();

}public static voidmain(String[] args){try{

System.out.println( get("http://127.0.0.1/crazy_java.pdf", null) );

}catch(Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值