废话不多说,直接上代码:
package org.changneng.framework.frameworkweb.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class HttpURLConnectionTest {
public static final String GET_URL = "http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&s=rsv3&city=35"
"&address=庄浪县朱店镇柳李村";
public static final String POST_URL = "http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&s=rsv3&city=35"
"&address=庄浪县朱店镇柳李村";
/**
* 接口调用 GET
*/
public static void httpURLConectionGET() {
try {
URL url = new URL(GET_URL); // 将字符串转换成URL请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
System.out.println("Get==" sb.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
}
/**
* 接口调用 POST
*/
public static void httpURLConnectionPOST () {
try {
URL url = new URL(POST_URL);
// 将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接 (标识一个url引用的远程对象连接)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中
// 连接输出流设置为true,默认false (post 请求是以流的形式隐式传递参数)
connection.setDoOutput(true);
// 连接输入流设置为true
connection.setDoInput(true);
// 设置请求的方式为post
connection.setRequestMethod("POST");
// post请求缓存设为false
connection.setUseCaches(false);
// 设置该HttpURLConnection实例是否自动执行重定向
connection.setInstanceFollowRedirects(true);
// 设置请求头中的每个属性 (以下是设置内容的类型,设置为通过urlEncoded编码过的from参数)
// application/x-javascript text/xml->xml数据
//application/x-javascript->json对象
//application/x-www-form-urlencoded->表单数据
//这是重点^_^
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 建立连接 (请求直到开始才开始connection.getInputStream()方法调用前启动,上述参数设置应在此方法前进行)
connection.connect();
// 创建输入输出流,用于输出连接中携带的参数(输出内容为以下内容)
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
String parm = "storeId=" URLEncoder.encode("32", "utf-8"); //URLEncoder.encode()方法 编码字符串
// 将参数输出到连接中
dataout.writeBytes(parm);
// 输出完成后,刷新并关闭流
dataout.flush();
dataout.close(); // 步骤且容易忽略步骤 (关闭流,记住!
System.out.println(connection.getResponseCode());
// 连接发起请求,处理服务器响应 (从连接到输入流并包装bufferedReader)
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder(); // 用于存储响应数据
// 循环读取流,如果不到最后
while ((line = bf.readLine()) != null) {
sb.append(bf.readLine());
}
bf.close(); // 步骤且容易忽略步骤 (关闭流,记住!
connection.disconnect(); // 销毁连接
System.out.println("Post==" sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// httpURLConectionGET();
httpURLConectionGET();
httpURLConnectionPOST();
}
}