实现代码
/**
* http 请求工具类
* Created by zhh on 2017/12/08.
*/
public class HttpHelper {
private final static int READ_BODY_SIZE = 5120 ;
/**
* post 方式请求内容
* @param strUrl url地址
* @param paramMap 参数集
* @return
*/
public static String post (String strUrl, Map<String, Object> paramMap) {
StringBuffer sb = new StringBuffer();
for (String strKey : paramMap.keySet()) {
sb.append("&" );
sb.append(strKey);
sb.append("=" );
sb.append(paramMap.get(strKey));
}
sb.replace(0 , 1 , "?" );
if (sb.length() > 0 ) {
return post(strUrl + sb.toString(), "" );
}
return post(strUrl + sb.toString(), "" );
}
/**
* get 方式请求内容
* @param strUrl url地址
* @param paramMap 参数集
* @return
*/
public static String get (String strUrl, Map<String, Object> paramMap) {
StringBuffer sb = new StringBuffer();
for (String strKey : paramMap.keySet()) {
sb.append("&" );
sb.append(strKey);
sb.append("=" );
sb.append(paramMap.get(strKey));
}
sb.replace(0 , 1 , "?" );
if (sb.length() > 0 ) {
return get(strUrl + sb.toString());
}
return get(strUrl + sb.toString());
}
/**
* post 方式请求内容
* @param strUrl url地址
* @param content 输出内容
* @return
*/
public static String post (String strUrl, String content) {
return post(strUrl, content, 180 );
}
/**
* get 方式请求内容
* @param strUrl url地址
* @return
*/
public static String get (String strUrl) {
return get(strUrl, 180 );
}
/**
* post 方式请求内容
* @param strUrl url地址
* @param content 输出内容
* @param timeoutSecond 设置延时时间
* @return
*/
public static String post (String strUrl, String content, int timeoutSecond) {
try {
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(timeoutSecond * 1000 );
con.setReadTimeout(timeoutSecond * 1000 );
con.setDoInput(true );
con.setDoOutput(true );
con.setAllowUserInteraction(false );
con.setUseCaches(true );
con.setRequestMethod("POST" );
con.setRequestProperty("Content-Type" , "application/x-www-form-urlencoded;charset=UTF-8" );
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
bout.write(content);
bout.flush();
bout.close();
return streamReadHtml(con.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
/**
* get 方式请求内容
* @param strUrl url地址
* @param timeoutSecond 设置延时时间
* @return
*/
public static String get (String strUrl, int timeoutSecond) {
try {
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(timeoutSecond * 1000 );
con.setReadTimeout(timeoutSecond * 1000 );
con.setRequestMethod("GET" );
con.setRequestProperty("Content-Type" , "application/x-www-form-urlencoded;charset=UTF-8" );
con.connect();
return streamReadHtml(con.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
/**
* 读取内容
* @param istream
* @return
* @throws UnsupportedEncodingException
*/
public static String streamReadHtml (InputStream istream) throws UnsupportedEncodingException {
String html = "" ;
byte [] responseBody = new byte [READ_BODY_SIZE];
int npos = 0 ;
int nread = 0 ;
try {
while ((nread = istream.read(responseBody, npos, responseBody.length - npos)) >= 0 ) {
npos += nread;
byte [] tmpBuf = new byte [npos + READ_BODY_SIZE];
System.arraycopy(responseBody, 0 , tmpBuf, 0 , npos);
responseBody = tmpBuf;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (istream != null ) {
istream.close();
}
istream = null ;
} catch (Exception e) {
e.printStackTrace();
}
}
html = new String(responseBody, 0 , npos, "UTF-8" );
return html;
}
}
代码用例
public static void main (String[] args) {
String reqUrl = "https://www.baidu.com" ;
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("wd" , "测试test" );
System.out.println(HttpHelper.post(reqUrl, paramMap));
}