import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
/**
* Java 调用HTTP接口 使用POST、GET 方法 传参工具
*
* @author czs
*
*/
public class HttpParam {
private String Url;
public HttpParam() {
}
public HttpParam(String Url) {
this.Url = Url;
}
public String Post() {
try {
URL url = new URL(Url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 在连接之前设置属性
// Content-Type实体头用于向接收方指示实体的介质类型,指定HEAD方法送到接收方的实体介质类型,或GET方法发送的请求介质类型
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
// 设置打开与此URLConnection引用的资源的通信链接时使用的指定超时值(以毫秒为单位)
conn.setConnectTimeout(10000);
// 将读取超时设置为指定的超时时间,以毫秒为单位。
// conn.setReadTimeout(60000);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
// Post 请求不能使用缓存
conn.setUseCaches(false);
// 建立连接
conn.connect();
// 传入参数
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
// 正文内容后的参数 "字段名=" + "字段值"
// String content =""+URLEncoder.encode("", "utf-8");
JSONObject jo = JSONObject.parseObject("");
String content = jo.toString();
dos.writeBytes(content);
// 关闭流
dos.flush();
dos.close();
// 获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String result = "";
while ((line = reader.readLine()) != null) {
result += line;
}
reader.close();
conn.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String Get() {
try {
// 传入参数
String realUrl = Url + "?" + "city=" + "北京";
URL url = new URL(realUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 在连接之前设置属性
// Content-Type实体头用于向接收方指示实体的介质类型,指定HEAD方法送到接收方的实体介质类型,或GET方法发送的请求介质类型
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
// 设置打开与此URLConnection引用的资源的通信链接时使用的指定超时值(以毫秒为单位)
conn.setConnectTimeout(10000);
// 将读取超时设置为指定的超时时间,以毫秒为单位。
// conn.setReadTimeout(60000);
conn.setRequestMethod("GET");
// Post 请求不能使用缓存
conn.setUseCaches(false);
// 建立连接
conn.connect();
// 获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String result = "";
while ((line = reader.readLine()) != null) {
result += line;
}
reader.close();
conn.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}