现在虽然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写法的更多相关文章

  1. Android入门(二十)HttpURLConnection与HttpClient

    原文链接:http://www.orlion.ga/679/ 在 Android上发送 HTTP请求的方式一般有两种,HttpURLConnection和 HttpClient. 一.HttpURLC ...

  2. Android回调接口的写法

    方法一: 定义一个接口,里面写想要对外提供的方法,在逻辑层方法的参数里传递进去,让在需要的时候调接口里的方法. 实例一: public class SmsUtils { public interfac ...

  3. 使用HttpURLConnection实现在android客户端和服务器之间传递对象

    一般情况下,客户端和服务端的数据交互都是使用json和XML,相比于XML,json更加轻量级,并且省流量,但是,无论我们用json还是用xml,都需要我们先将数据封装成json字符串或者是一个xml ...

  4. 关于HttpClient,HttpURLConnection,OkHttp的用法

    1 HttpClient入门实例 1.1发送get请求 /** * HttpClient发送get请求 * @param url 请求地址 * @return * @throws IOExceptio ...

  5. Java HttpURLConnection模拟请求Rest接口解决中文乱码问题

    转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...

  6. 转 关于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 ...

  7. HttpUrlConnection 基础使用

    From https://developer.android.com/reference/java/net/HttpURLConnection.html HttpUrlConnection: A UR ...

  8. obj.style.z-index的正确写法

    obj.style.z-index的正确写法 今天发现obj.style.z-index在js里面报错,后来才知道在js里应该把含"-"的字符写成驼峰式,例如obj.style.z ...

  9. java设计模式之单例模式(几种写法及比较)

    概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...

随机推荐

  1. Points on cycle

    Description There is a cycle with its center on the origin. Now give you a point on the cycle, you a ...

  2. Apache 常用伪静态配置

    1. /a/b?c=d => index.php?_a=a&_m=b&c=d 2. /xxx/detail-yyy.html => index.php?_a=xxx& ...

  3. horizon 修改local的logging 配置

    再部署完horizon的开发环境后,首先要做的就是修改下logging的输出. 我用的开发软件是pycharm, 所以,为了方便在 console里看到输出.需要在 /home/geiao/repo/ ...

  4. Win7 远程 Ubuntu 桌面 mate desktop,并实现中文输入法

    一. 安装mate desktop: $sudo apt-add-repository ppa:ubuntu-mate-dev/ppa $sudo apt-add-repository ppa:ubu ...

  5. ubuntn下 nginx+phpstorm 中配置xdebug调试

    xdebug安装和配置说明,主要用于个人学习记录. 一.echo phpinfo(); 搜素xdebug,若未搜素到,则标识未安装或安装失败. 二.拷贝步骤1中输出的所有结果.访问http://xde ...

  6. 125 Valid Palindrome

    public class Solution { public boolean isPalindrome(String s) { if(s==null) return false; s=s.toLowe ...

  7. SQL 注入

    我们的团队项目中有课程名称输入框,其中的内容会拼接到类sql查询语句中. 所以可能会产生类sql注入的问题,我们团队采用了利用正则表达式判断输入内容的形式来规避这类注入. 下面简单介绍一下sql注入 ...

  8. openVPN报错:All TAP-Windows adapters on this system are currently in use

    解决办法: 1. 确定openVPN服务已打开. 2. 控制面板\网络和 Internet\网络连接,确定TAP-Windows Adapter V9已经启动.

  9. jenkins2 groovy语法

    文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd   安装: wget https://dl.bin ...

  10. paip.哈米架构CAO.txt

    paip.哈米架构CAO.txt 智能语义搜索方法,建立了学习机,通过对用户点击情况的处理,对知识库进行及时更新,并将搜索结果根据关联度进行排序及重新排序,使用户能快速查询信息,从而能不断适应用户使用 ...