java http连接 get post

来源:互联网 发布:西瓜影音mac官方下载 编辑:程序博客网 时间:2024/05/21 10:32
package cn.fht.car.utils.java;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.Map;import java.util.Map.Entry;import java.util.Set;public class HttpGetUtils {private static String getParam(Map<String, String> map)throws UnsupportedEncodingException {String param = null;if (map != null && map.keySet().size() > 0) {StringBuffer sb = new StringBuffer();Set<Entry<String, String>> keyset = map.entrySet();for (Entry<String, String> entry : keyset) {sb.append(entry.getKey());sb.append("=");sb.append(URLEncoder.encode(entry.getValue(), "utf-8"));sb.append("&");}param = sb.toString();param = param.substring(0, param.length() - 1);}return param;}/** * 根据url 及参数返回网页内容 。<Br> * 普通方式<Br> * 提交的格式为utf-8,服务器端接收到参数 要考虑转码的问题 *  * @param url * @param map *            map类型的参数 url后面的参数 * @return * @throws Exception */public static String submitGet(String url, Map<String, String> map)throws Exception {String result = "";url = getUrl(url, map);System.out.println(url);URL u = null;u = new URL(url);InputStream is = null;HttpURLConnection conn = null;conn = (HttpURLConnection) u.openConnection();conn.setConnectTimeout(30 * 1000);conn.setReadTimeout(30 * 1000);is = conn.getInputStream();ByteArrayOutputStream bos = new ByteArrayOutputStream();int readsize = 0;byte[] b = new byte[1024];while (-1 != (readsize = is.read(b, 0, b.length))) {bos.write(b, 0, readsize);}result = new String(bos.toByteArray());// bos.toString();bos.close();is.close();conn.disconnect();return result;}/** * java.net实现 HTTP POST方法提交 *  * @param url * @param paramContent * @param map * @return * @throws IOException */public static String submitPost(String url, Map<String, String> params)throws IOException {HttpURLConnection con = null;// 构建请求参数String sb = getParam(params);// 尝试发送请求URL u = new URL(url);con = (HttpURLConnection) u.openConnection();con.setRequestMethod("POST");con.setDoOutput(true);con.setDoInput(true);con.setUseCaches(false);con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(),"UTF-8");if (sb != null) {osw.write(sb);}osw.flush();osw.close();if (con != null) {con.disconnect();}// 读取返回内容ByteArrayOutputStream bos = new ByteArrayOutputStream();int readsize = 0;byte[] b = new byte[1024];InputStream is = con.getInputStream();while (-1 != (readsize = is.read(b, 0, b.length))) {bos.write(b, 0, readsize);}String result = new String(bos.toByteArray());//,"GBK");// bos.toString();bos.close();is.close();return result;}/** * 拼接 url *  * @param url *            根url * @param map *            参数 * @return * @throws UnsupportedEncodingException */private static String getUrl(String url, Map<String, String> map)throws UnsupportedEncodingException {String param = getParam(map);if (param != null) {url = url + "?" + param;}return url;}}

0 0
原创粉丝点击