HttpURLConnection GET/POST写法
现在虽然HttpClient很好使,但也有人在用最原生的HttpURLConnection, 记录一下,备忘之。
public class HttpUrlConnect {
//get请求
public String get(String url){
HttpURLConnection conn = null;
BufferedReader rd = null ;
StringBuilder sb = new StringBuilder ();
String line = null ;
String response = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
conn.connect();
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
//post表单请求
public String post(String url, Map<String, String> form){
HttpURLConnection conn = null;
PrintWriter pw = null ;
BufferedReader rd = null ;
StringBuilder out = new StringBuilder();
StringBuilder sb = new StringBuilder();
String line = null ;
String response = null;
for (String key : form.keySet()) {
if(out.length()!=0){
out.append("&");
}
out.append(key).append("=").append(form.get(key));
}
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
conn.connect();
pw = new PrintWriter(conn.getOutputStream());
pw.print(out.toString());
pw.flush();
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(pw != null){
pw.close();
}
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
//post字符串请求
public String post(String url, String rawBody){
HttpURLConnection conn = null;
PrintWriter pw = null ;
BufferedReader rd = null ;
StringBuilder sb = new StringBuilder ();
String line = null ;
String response = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
conn.connect();
pw = new PrintWriter(conn.getOutputStream());
pw.print(rawBody);
pw.flush();
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(pw != null){
pw.close();
}
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
public static void main(String[] args) {
HttpUrlConnect h = new HttpUrlConnect();
System.out.println(h.get(""));
Map<String, String> form = new HashMap<String, String>();
form.put("test", "test");
System.out.println(h.post("", form));
}
}
HttpURLConnection GET/POST写法的更多相关文章
- Android入门(二十)HttpURLConnection与HttpClient
原文链接:http://www.orlion.ga/679/ 在 Android上发送 HTTP请求的方式一般有两种,HttpURLConnection和 HttpClient. 一.HttpURLC ...
- Android回调接口的写法
方法一: 定义一个接口,里面写想要对外提供的方法,在逻辑层方法的参数里传递进去,让在需要的时候调接口里的方法. 实例一: public class SmsUtils { public interfac ...
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
一般情况下,客户端和服务端的数据交互都是使用json和XML,相比于XML,json更加轻量级,并且省流量,但是,无论我们用json还是用xml,都需要我们先将数据封装成json字符串或者是一个xml ...
- 关于HttpClient,HttpURLConnection,OkHttp的用法
1 HttpClient入门实例 1.1发送get请求 /** * HttpClient发送get请求 * @param url 请求地址 * @return * @throws IOExceptio ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...
- 转 关于HttpClient,HttpURLConnection,OkHttp的用法
转自:https://www.cnblogs.com/zp-uestc/p/10371012.html 1 HttpClient入门实例 1.1发送get请求 1 2 3 4 5 6 7 8 9 10 ...
- HttpUrlConnection 基础使用
From https://developer.android.com/reference/java/net/HttpURLConnection.html HttpUrlConnection: A UR ...
- obj.style.z-index的正确写法
obj.style.z-index的正确写法 今天发现obj.style.z-index在js里面报错,后来才知道在js里应该把含"-"的字符写成驼峰式,例如obj.style.z ...
- java设计模式之单例模式(几种写法及比较)
概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...
随机推荐
- nginx 从vagant挂载目录中加载nginx.conf配置进行开机启动
nginx从vagrant挂载目录中读取配置启动,将nginx加入开机启动项!开机启动的时候nginx会因为加载不了配置导致启动失败! 原因是开机启动nginx服务在vagrant挂载之前,导致无法正 ...
- iOS AFNetWorking 下载pdf文档
+ (void)downLoadPdf:(NSString *)url pdf_id:(NSString *)pdf_id block:(APIFilePath)pdfFilePath { NS ...
- 5.3 Static
相信很多人都遇到过App 莫名其妙就崩溃的情况,尤其是一些配置很低的手机,重现场景就是在App 切换到后台,闲置了一段时间后再继续使用时,就会崩溃.导致上述崩溃发生的罪魁祸首就是全局变量. 在内存不足 ...
- JS检测浏览器是否支持HTML5视频播放 (标签<video>) ,
function checkVideo() { if (!!document.createElement('video').canPlayType) { var vidTest = document. ...
- Linq To Object
//SelectMany List<List<int>> Numbers = new List<List<int>>() { new List<i ...
- 命令行查看linux发行版版本信息
有时候安装完自己的linux发行版系统(如ubuntu.centos.redhat.opensuse.--)时,把版本信息忽略了,又不想重启电脑,此时我们可以通过命令行方式来查看: 1.cat /et ...
- Largest Rectangle in a Histogram(HDU1506)
Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...
- 【基础知识】.Net基础加强09天
委托: 1. 委托是一种数据类型,像类一样{可以声明委托变量类型} 2. deleate关键字定义委托 : public delegate void MethodDelegate();//这就是定义了 ...
- c# 高效读写文件
一.同步读写文件(在并发情况下不会发生文件被占用异常) static void Main(string[] args) { Parallel.For(0, 10000, e => { strin ...
- 用c#开发微信(1)服务号的服务器配置和企业号的回调模式 - url接入 (源码下载)
最近研究了下服务号的服务器配置和企业号的回调模式.真正实现完后,觉得很简单,但一开始还是走了点弯路,所以写了个web程序,只用改下配置文件里的参数就可以直接用了.下面介绍下详细的用法以及实现步骤. 本 ...