java發(fā)送http的get、post請(qǐng)求

轉(zhuǎn)自:?java發(fā)送http的get、post請(qǐng)求

Http請(qǐng)求類

package wzh.Http;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

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) {

? ? ? ? String result = "";

? ? ? ? BufferedReader in = null;

? ? ? ? try {

? ? ? ? ? ? String urlNameString = url + "?" + param;

? ? ? ? ? ? URL realUrl = new URL(urlNameString);

? ? ? ? ? ? // 打開(kāi)和URL之間的連接

? ? ? ? ? ? URLConnection connection = realUrl.openConnection();

? ? ? ? ? ? // 設(shè)置通用的請(qǐng)求屬性

? ? ? ? ? ? connection.setRequestProperty("accept", "*/*");

? ? ? ? ? ? connection.setRequestProperty("connection", "Keep-Alive");

? ? ? ? ? ? connection.setRequestProperty("user-agent",

? ? ? ? ? ? ? ? ? ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

? ? ? ? ? ? // 建立實(shí)際的連接

? ? ? ? ? ? connection.connect();

? ? ? ? ? ? // 獲取所有響應(yīng)頭字段

? ? ? ? ? ? Map<String, List<String>> map = connection.getHeaderFields();

? ? ? ? ? ? // 遍歷所有的響應(yīng)頭字段

? ? ? ? ? ? for (String key : map.keySet()) {

? ? ? ? ? ? ? ? System.out.println(key + "--->" + map.get(key));

? ? ? ? ? ? }

? ? ? ? ? ? // 定義 BufferedReader輸入流來(lái)讀取URL的響應(yīng)

? ? ? ? ? ? in = new BufferedReader(new InputStreamReader(

? ? ? ? ? ? ? ? ? ? connection.getInputStream()));

? ? ? ? ? ? String line;

? ? ? ? ? ? while ((line = in.readLine()) != null) {

? ? ? ? ? ? ? ? result += line;

? ? ? ? ? ? }

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? System.out.println("發(fā)送GET請(qǐng)求出現(xiàn)異常!" + e);

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? // 使用finally塊來(lái)關(guān)閉輸入流

? ? ? ? finally {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? if (in != null) {

? ? ? ? ? ? ? ? ? ? in.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } catch (Exception e2) {

? ? ? ? ? ? ? ? e2.printStackTrace();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return result;

? ? }

? ? /**

? ? * 向指定 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;

? ? ? ? String result = "";

? ? ? ? try {

? ? ? ? ? ? URL realUrl = new URL(url);

? ? ? ? ? ? // 打開(kāi)和URL之間的連接

? ? ? ? ? ? URLConnection conn = realUrl.openConnection();

? ? ? ? ? ? // 設(shè)置通用的請(qǐng)求屬性

? ? ? ? ? ? conn.setRequestProperty("accept", "*/*");

? ? ? ? ? ? conn.setRequestProperty("connection", "Keep-Alive");

? ? ? ? ? ? conn.setRequestProperty("user-agent",

? ? ? ? ? ? ? ? ? ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

? ? ? ? ? ? // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行

? ? ? ? ? ? conn.setDoOutput(true);

? ? ? ? ? ? conn.setDoInput(true);

? ? ? ? ? ? // 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流

? ? ? ? ? ? out = new PrintWriter(conn.getOutputStream());

? ? ? ? ? ? // 發(fā)送請(qǐng)求參數(shù)

? ? ? ? ? ? out.print(param);

? ? ? ? ? ? // flush輸出流的緩沖

? ? ? ? ? ? out.flush();

? ? ? ? ? ? // 定義BufferedReader輸入流來(lái)讀取URL的響應(yīng)

? ? ? ? ? ? in = new BufferedReader(

? ? ? ? ? ? ? ? ? ? new InputStreamReader(conn.getInputStream()));

? ? ? ? ? ? String line;

? ? ? ? ? ? while ((line = in.readLine()) != null) {

? ? ? ? ? ? ? ? result += line;

? ? ? ? ? ? }

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? System.out.println("發(fā)送 POST 請(qǐng)求出現(xiàn)異常!"+e);

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? //使用finally塊來(lái)關(guān)閉輸出流、輸入流

? ? ? ? finally{

? ? ? ? ? ? try{

? ? ? ? ? ? ? ? if(out!=null){

? ? ? ? ? ? ? ? ? ? out.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if(in!=null){

? ? ? ? ? ? ? ? ? ? in.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? catch(IOException ex){

? ? ? ? ? ? ? ? ex.printStackTrace();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return result;

? ? }? ?

}

調(diào)用方法:

public static void main(String[] args) {

? ? ? ? //發(fā)送 GET 請(qǐng)求

? ? ? ? String s=HttpRequest.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456");

? ? ? ? System.out.println(s);


? ? ? ? //發(fā)送 POST 請(qǐng)求

? ? ? ? String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");

? ? ? ? System.out.println(sr);

? ? }

本文地址:http://www.itdecent.cn/p/76d3a0a84fd9

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容