程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java收集編程中向指定URL發送GET POST要求示例

java收集編程中向指定URL發送GET POST要求示例

編輯:關於JAVA

java收集編程中向指定URL發送GET POST要求示例。本站提示廣大學習愛好者:(java收集編程中向指定URL發送GET POST要求示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java收集編程中向指定URL發送GET POST要求示例正文



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpRequest {
    /**
     * 向指定URL發送GET辦法的要求
     *
     * @param url
     *            發送要求的URL
     * @param param
     *            要求參數,要求參數應當是 name1=value1&name2=value2 的情勢。
     * @return URL 所代表長途資本的呼應成果
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 翻開和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)); }
             */
            // 界說 BufferedReader輸出流來讀取URL的呼應
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發送GET要求湧現異常!" + e);
            e.printStackTrace();
        }
        // 應用finally塊來封閉輸出流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 向指定 URL 發送POST辦法的要求
     *
     * @param url
     *            發送要求的 URL
     * @param param
     *            要求參數,要求參數應當是 name1=value1&name2=value2 的情勢。
     * @return 所代表長途資本的呼應成果
     */
    public 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;
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved