JavaWeb之傳送Get請求和Post請求的封裝類
阿新 • • 發佈:2018-12-10
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* @author 作者 macx:
* @version 建立時間:2018年7月12日 下午2:20:47 類說明
*/
@SuppressWarnings({"deprecation"})
public class Test_1 {
public static void main(String[] args) {
sendPost("","params");
}
/**
* 向指定 URL 傳送POST方法的請求
*/
private static String sendPost(String url, String param) {
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", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 傳送POST請求必須設定如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection物件對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 傳送請求引數
out.print(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;
}
/**
* 向指定 URL 傳送GET方法的請求
*/
JSONObject jsonResult = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
// 發get請求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/** 請求發求成功,並得到響應 **/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/** 讀取伺服器返回過來的json字串數 **/
String strResult = EntityUtils.toString(response.getEntity());
System.out.println(strResult);
/** 把json字串轉換成json物件 **/
try {
jsonResult = JSONObject.fromObject(strResult);
url = URLDecoder.decode(url, "UTF-8");
} catch (Exception e) {
jsonResult = new JSONObject();
jsonResult.put("id", strResult);
}
} else {
System.out.println("get請求提交失敗:" + url);
}
((Closeable) client).close();
} catch (IOException e) {
System.out.println("get請求提交失敗:" + url);
}
return jsonResult;
}
}
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* @author 作者 macx:
* @version 建立時間:2018年7月12日 下午2:20:47 類說明
*/
@SuppressWarnings({"deprecation"})
public class Test_1 {
public static void main(String[] args) {
sendPost("","params");
}
/**
* 向指定 URL 傳送POST方法的請求
*/
private static String sendPost(String url, String param) {
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", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 傳送POST請求必須設定如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection物件對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 傳送請求引數
out.print(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;
}
* 向指定 URL 傳送GET方法的請求
*/
public static JSONObject httpGet(String url) {
JSONObject jsonResult = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
// 發get請求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/** 請求發求成功,並得到響應 **/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/** 讀取伺服器返回過來的json字串數 **/
String strResult = EntityUtils.toString(response.getEntity());
System.out.println(strResult);
/** 把json字串轉換成json物件 **/
try {
jsonResult = JSONObject.fromObject(strResult);
url = URLDecoder.decode(url, "UTF-8");
} catch (Exception e) {
jsonResult = new JSONObject();
jsonResult.put("id", strResult);
}
} else {
System.out.println("get請求提交失敗:" + url);
}
((Closeable) client).close();
} catch (IOException e) {
System.out.println("get請求提交失敗:" + url);
}
return jsonResult;
}
}