Java发起Post请求的两种方式
2015-02-27 16:41
274 查看
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpUtils { private static final Logger logger = LoggerFactory .getLogger(HttpUtils.class); public static String doPost(String url, Map<String, String> params) { String response = null; HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(url); // 设置Http Post数据 if (params != null) { HttpMethodParams p = new HttpMethodParams(); for (Map.Entry<String, String> entry : params.entrySet()) { p.setParameter(entry.getKey(), entry.getValue()); } method.setParams(p); } method.setRequestHeader("accept", "*/*"); method.setRequestHeader("connection", "Keep-Alive"); method.setRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } else { logger.error("statusCode:{}", method.getStatusCode()); } } catch (IOException e) { logger.error("{}", e); } finally { method.releaseConnection(); } return response; } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param params * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String doPost(String url, String params) { PrintWriter out = null; BufferedReader in = null; StringBuilder result = new StringBuilder(); try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(params); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line = null; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { logger.error("{}", e); } finally { // 使用finally块来关闭输出流、输入流 if (out != null) { out.close(); } try { if (in != null) { in.close(); } } catch (IOException e) { logger.error("{}", e); } } return result.toString(); } public static void main(String[] args) { } }
相关文章推荐
- [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式
- java中两种发起POST请求,并接收返回的响应内容的方式 (转)
- Java网络请求(get/post)工具类实现的两种方式
- java发送http的两种请求方式——get、post
- Ajax中的get和post两种请求方式的用法
- Ajax中的get和post两种请求方式的异同(转)
- java 用面向对象的方式发送http的GET和POST请求
- HTTP的两种提交请求的方式get和post
- 设置java.net.URL请求方式post get
- Ajax中的get和post两种请求方式的异同
- Ajax的get和post两种请求方式区别
- GET和POST两种请求方式的区别
- Ajax中的get和post两种请求方式的异同
- 用Delphi的TIdHttp控件发起POST请求和Java的Servlet响应
- Ajax中的get和post两种请求方式的异同
- Ajax中get和post两种请求方式的用法
- android中post和get两种方式发送请求
- java中httpclent以post方式提交请求时,http返回码是302重定向时,不自动跳转的处理
- java发送post和get请求(1)---HttpURLConnection方式
- android 向服务器Get和Post请求的两种方式,android向服务器发送文件,自己组装协议和借助第三方开源