Http Request Response

package com.travelsky.net.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class NetTool {

/**
* 发送Post请求
* @param basePath 请求路径
* @param params 请求参数 key为参数名称,value为参数值
* @return
* @throws Exception
*/
public static InputStream sendPostRequest(String basePath, Map<String, String> params, String encoding) throws Exception {
StringBuilder sb = new StringBuilder();
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), encoding));
sb.append('&');
}
sb.deleteCharAt(sb.length()-1);
byte[] data = (sb.toString()).getBytes();
URL url = new URL(basePath);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(6*1000);
conn.setDoOutput(true);//发送POST请求必须设置允许输出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");//维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
//application不能上传文件,如果要上传文件则 multipart
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

DataOutputStream dataOutStream = new DataOutputStream(conn.getOutputStream());
dataOutStream.write(data);
dataOutStream.flush();
dataOutStream.close();
if(200 == conn.getResponseCode()){
return conn.getInputStream();
}
return null;
}

/**
* 获取Url路径指定的内容
*
* @param basePath
* @param encoding
* @return
* @throws Exception
*/
public static String getTextContent(InputStream inStream, String encoding)
throws Exception {
byte[] data = readStream(inStream);
return new String(data, encoding);

}

/**
* 获取Url路径指定的内容
*
* @param basePath
* @param encoding
* @return
* @throws Exception
*/
public static InputStream getContent(String basePath)
throws Exception {
URL url = new URL(basePath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);
if (200 == conn.getResponseCode()) {
return conn.getInputStream();
}
return null;
}

/**
* 获取给的Url路径的数据
*
* @param basePath
*            Url路径
* @return
* @throws Exception
*/
public static byte[] getImage(String basePath) throws Exception {
URL url = new URL(basePath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);
System.err.println(conn.getResponseCode());
if (200 == conn.getResponseCode()) {
InputStream inStream = conn.getInputStream();
byte[] data = readStream(inStream);
return data;
}
return null;
}

/**
* 读取数据
*
* @param inStream
*            输入流
* @return
* @throws Exception
*/
private static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
}

猜你喜欢

转载自wtmax.iteye.com/blog/1222644
今日推荐