一、发送Get请求
ublic static String sendGet(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
String reaUrl = url + "?" + param;
try {
URL realUrl = new URL(reaUrl);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//建立真正的连接
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for(String key : map.keySet()) {
System.out.println("key -->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
while ((line = in.readLine()) != null) {
result += line + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
二、发送post请求
public static String sendPost(String url, String string) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realurl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realurl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "KeepAlive");
conn.setRequestProperty("user-agent", "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true); //由于URLConnection在默认情况下不允许输出,所以在请求输出流之前必须将其设置为true,此时请求方式由get变为post
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(string);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line = in.readLine()) != null) {
result += line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
三、通过post请求发送json数据
/**
* 参数json为json格式的字符串
* @param urlStr
* @param json
* @return
*/
public static String sendPostByJSON(String urlStr, String json) {
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
String response = "";
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST"); //POST需大写
connection.setRequestProperty("content-Type", "application/json"); //设置数据格式为json
connection.setRequestProperty("charset", "utf-8"); //设置编码格式为utf-8
connection.connect();
outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.append(json);
outputStreamWriter.flush();
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line = "";
while((line = bufferedReader.readLine()) != null) {
response += line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStreamWriter.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
四、通过代理发送post请求
public static String sendPost(String url, String param,boolean isproxy) {
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
HttpURLConnection conn = null;
if(isproxy){//使用代理模式
@SuppressWarnings("static-access")
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));//proxyHost和proxyPort是代理服务器的地址和端口号
conn = (HttpURLConnection) realUrl.openConnection(proxy);
}else{
conn = (HttpURLConnection) realUrl.openConnection();
}
// 打开和URL之间的连接
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST"); // POST方法
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.connect();
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}