Java使用HttpURLConnection发送POST和GET请求
2017-12-14 17:49
901 查看
代码示例
/** * 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); // 设置从HttpURLConnection读入 con.setDoInput(true); // 设置是否向httpUrlConnection输出, 因为这个是post请求, 参数要放在http正文内, 因此需要设为true, 默认情况下是false con.setDoOutput(true); // 设置该URLConnection的allowUserInteraction请求头字段的值 con.setAllowUserInteraction(false); con.setUseCaches(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // 此处getOutputStream会隐含的进行connect,所以在开发中不调用上述的connect()也可以 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", "测试"); System.out.println(HttpHelper.post(reqUrl, paramMap)); }
相关文章推荐
- Java使用HttpURLConnection发送Get及Post请求简单过程
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- 在JAVA中使用HttpURLConnection发送POST请求
- Http学习之使用HttpURLConnection发送post和get请求(2)
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求 android
- java发送post和get请求(1)---HttpURLConnection方式
- java使用HttpURLConnection和HttpClient分别模拟get和post请求以及操作cookies
- 使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求(转)
- IT忍者神龟之使用HttpURLConnection向服务器发送post和get请求
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应
- Http学习之使用HttpURLConnection发送post和get请求 .
- Http学习之使用HttpURLConnection发送post和get请求(1)
- Http学习之使用HttpURLConnection发送post和get请求(3)
- Http学习之使用HttpURLConnection发送post和get请求
- 使用HttpURLConnection向服务器发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求