使用http連接創(chuàng)建,發(fā)送post和get請(qǐng)求
/**
* 作用 :
* http request的java工具類憎蛤;
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
/**
* 向指定URL發(fā)送GET方法的請(qǐng)求
*
* @param url
* 發(fā)送請(qǐng)求的URL
* @param param
* 請(qǐng)求參數(shù)外傅,請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。
* @return URL 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
*/
public static String sendGet(String url, String param) {
BufferedReader in = null;
StringBuffer sbRes = new StringBuffer();
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 設(shè)置通用的請(qǐng)求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
if (HttpURLConnection.HTTP_OK != conn.getResponseCode()) {
throw new Exception("get failed ErrorCode="+conn.getResponseCode());
}
// 定義 BufferedReader輸入流來(lái)讀取URL的響應(yīng)
in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sbRes.append(line).append("\r\n");
}
}catch (MalformedURLException e) {
System.out.println("Get failed.The specified URL " + param
+ " is not a valid URL. Please check");
e.printStackTrace();
System.out.println(url+param);
sbRes.append("get failed,param:"+param);
} catch (IOException e) {
System.out.println("Get failed.An error occured. Please check that Solr is running.");
e.printStackTrace();
System.out.println(url+param);
sbRes.append("get failed,param:"+param);
}catch (Exception e) {
e.printStackTrace();
System.out.println(url+param);
sbRes.append("get failed,param:"+param);
}
// 使用finally塊來(lái)關(guān)閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return sbRes.toString();
}
/**
* 向指定 URL 發(fā)送POST方法的請(qǐng)求
*
* @param url
* 發(fā)送請(qǐng)求的 URL
* @param param
* 請(qǐng)求參數(shù)俩檬,請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式萎胰。
* @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
StringBuffer sbRes = new StringBuffer();
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
// 設(shè)置通用的請(qǐng)求屬性
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// conn.setDoInput(true);
// conn.setUseCaches(false);
// conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-type", "text/json");
// conn.setChunkedStreamingMode(1024*50);
// 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
// 發(fā)送請(qǐng)求參數(shù)
out.print(param);
// flush輸出流的緩沖
out.flush();
if (HttpURLConnection.HTTP_OK != conn.getResponseCode()) {
if (conn.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST
|| conn.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
throw new Exception("post failed ErrorCode="+conn.getResponseCode());
}
System.out.println("ErrorCode="+conn.getResponseCode());
}
// 定義BufferedReader輸入流來(lái)讀取URL的響應(yīng)
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sbRes.append(line).append("\r\n");
}
} catch (Exception e) {
System.out.println("發(fā)送 POST 請(qǐng)求出現(xiàn)異常!"+e);
e.printStackTrace();
sbRes.append("post failed");
}
//使用finally塊來(lái)關(guān)閉輸出流棚辽、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return sbRes.toString();
}
}