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
- java http连接 get post
- JAVA HTTP GET/POST
- java HTTP GET POST
- Http连接GET/POST请求
- Http连接GET/POST请求
- Java socket HTTP(POST/GET)
- java http get、post方式
- Java--Http: HttpClient(post/get)
- java发送HTTP GET POST请求
- java发送get、post http请求
- java模拟http get和post 提交
- java,http的post和get
- java实现http的post和get
- java发送http的get、post请求
- java模拟http get和post 提交
- java发送http的get、post请求
- JAVA通过HTTP访问:Post+Get方式
- java发送http的get、post请求
- 程序员Web面试之jQuery
- @SuppressWarnings()的参数
- 拥抱开源,DNSPod推出基于Graphite协议的云监控服务
- FLEX4 URLLoader与后台交互请求XML文件
- Quickreport 可使用从VCL TDataset 类继承下来的任何数据组件
- java http连接 get post
- 一元线性同余方程&&一元线性m同余方程组(模板)
- Hadoop 新 MapReduce 框架 Yarn 详解
- Lua string 库
- flex4 与后台服务器通信方式:URLRequest+URLLoader JAVA方式
- 计应121--电子商务论文--胡俊琛--2012366161113
- 如何配置Qt使用VS2010进行开发
- 程序员必须知道的10大基础实用算法及其讲解
- Linux系统IPv6配置