向Web網站發送GET請求,POST請求,並從Web網站取得相應資料樣本,webget

來源:互聯網
上載者:User

向Web網站發送GET請求,POST請求,並從Web網站取得相應資料樣本,webget


package cn.internet.demo;import java.io.BufferedReader;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 GetPostTest {/** * 向指定URL發送GET方式請求 */public static String sendGet(String url,String param){String result = "";String urlName = url+"?"+param;try {URL realUrl = new URL(urlName);// 開啟聯結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)");// 建立實際的聯結conn.connect();System.out.println("檔案大小:"+conn.getContentLength());// 擷取所有的相應頭欄位Map<String, List<String>> map = conn.getHeaderFields();// 遍曆所有的相應頭欄位for (String key:map.keySet()) {System.out.println(key+"--->"+map.get(key));}// 定義 BufferedReader輸入資料流來讀取URL的相應try(BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"))){String line;while ((line = in.readLine())!=null) {result += "\n"+line;}}} catch (Exception e) {System.out.println("發送Get請求出現異常!");e.printStackTrace();}return result;}public static String sendPost(String url,String param){String result = "";try {URL realUrl = new 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對象對應的輸出資料流try(PrintWriter out = new PrintWriter(conn.getOutputStream())){out.print(param);// 發送請求參數out.flush();// flush輸出資料流的緩衝}// 定義 BufferedReader輸入資料流來讀取URL的相應try(BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"))){String line;while ((line = in.readLine())!=null) {result += "\n"+line;}}// 擷取所有的相應頭欄位Map<String, List<String>> map = conn.getHeaderFields();// 遍曆所有的相應頭欄位for (String key:map.keySet()) {System.out.println(key+"--->"+map.get(key));}} catch (Exception e) {System.out.println("發送Get請求出現異常!");e.printStackTrace();}return result;}public static void main(String[] args) {//String s = GetPostTest.sendGet("http://localhost:8080/NetWork_Code/index.jsp", "name=rapido&pwd=liying5201314");//System.out.println("get: "+s);String s = GetPostTest.sendGet("http://localhost:8080/NetWork_Code/data.json", null);System.out.println("get: "+s);//String s2 = GetPostTest.sendPost("http://localhost:8080/NetWork_Code/index.jsp", "name=rapido&pwd=liying5201314");//System.out.println("post: "+s2);}}
在Web應用程式中測試一個index.jsp頁面聯結

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String name = request.getParameter("name");String pwd = request.getParameter("pwd");System.out.println("name:" + name + ",pwd:" + pwd);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"></head><body>This is my JSP page.<h1 style="color:red">使用者名稱:<%=name %>, 密碼:<%=pwd %></h1><br></body></html>
啟動服務後,執行main方法返回以下類型資料
檔案大小:539null--->[HTTP/1.1 200 OK]Date--->[Sat, 30 May 2015 14:57:58 GMT]Content-Length--->[539]Set-Cookie--->[JSESSIONID=CD19ECBE035FDC1287CE22AD5CEF6DBA; Path=/NetWork_Code/; HttpOnly]Content-Type--->[text/html;charset=UTF-8]Server--->[Apache-Coyote/1.1]get: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"></head><body>This is my JSP page.<h1 style="color:red">使用者名稱:rapido, 密碼:liying5201314</h1><br></body></html>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.