程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java若何完成URL帶要求參數(get/post)及獲得get和post要求url和參數列表的辦法

Java若何完成URL帶要求參數(get/post)及獲得get和post要求url和參數列表的辦法

編輯:關於JAVA

Java若何完成URL帶要求參數(get/post)及獲得get和post要求url和參數列表的辦法。本站提示廣大學習愛好者:(Java若何完成URL帶要求參數(get/post)及獲得get和post要求url和參數列表的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java若何完成URL帶要求參數(get/post)及獲得get和post要求url和參數列表的辦法正文


詳細代碼以下所示:

public static String sendGet(String url,String param)
 {
 String result = "";
 try{
 String urlName = url + "?"+param;//
 URL U = new URL(urlName);
 URLConnection connection = U.openConnection();
 connection.connect();
 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
 String line;
 while ((line = in.readLine())!= null)
 {
 result += line;
 }
 in.close(); 
 }catch(Exception e){
 System.out.println("Helloword!!"+e);
 }
 return result;
 }
 public static String sendPost(String url,String param)
 {
 String result="";
 try{
 URL httpurl = new URL(url);
 HttpURLConnection httpConn = (HttpURLConnection)httpurl.openConnection();  
 httpConn.setDoOutput(true);
 httpConn.setDoInput(true);
 PrintWriter out = new PrintWriter(httpConn.getOutputStream());
 out.print(param);
 out.flush();
 out.close();
 BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
 String line;
 while ((line = in.readLine())!= null)
 {
 result += line; 
 }
 in.close();
 }catch(Exception e){
 System.out.println("Helloword!"+e);
 }
 return result;
 }

上面給年夜家引見 java獲得GET和POST要求URL和參數列表的辦法

  在servlet中GET要求可以經由過程HttpServletRequest的getRequestURL辦法和getQueryString()獲得完全的要求途徑和要求一切參數列表,POST的須要getParameterMap()辦法遍歷獲得,豈論GET或POST都可以經由過程getRequestURL+getParameterMap()來獲得要求完全途徑

package com.zuidaima       
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Map; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class GetParams extends HttpServlet { 
 private static final long serialVersionUID = 1L; 
 public GetParams() { 
  super(); 
 } 
 protected void doGet(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException, IOException { 
  PrintWriter writer = response.getWriter(); 
  writer.println("GET " + request.getRequestURL() + " " 
    + request.getQueryString()); 
  Map<String, String[]> params = request.getParameterMap(); 
  String queryString = ""; 
  for (String key : params.keySet()) { 
   String[] values = params.get(key); 
   for (int i = 0; i < values.length; i++) { 
    String value = values[i]; 
    queryString += key + "=" + value + "&"; 
   } 
  } 
  // 去失落最初一個空格 
  queryString = queryString.substring(0, queryString.length() - 1); 
  writer.println("GET " + request.getRequestURL() + " " + queryString); 
 } 
 protected void doPost(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException, IOException { 
  PrintWriter writer = response.getWriter(); 
  Map<String, String[]> params = request.getParameterMap(); 
  String queryString = ""; 
  for (String key : params.keySet()) { 
   String[] values = params.get(key); 
   for (int i = 0; i < values.length; i++) { 
    String value = values[i]; 
    queryString += key + "=" + value + "&"; 
   } 
  } 
  // 去失落最初一個空格 
  queryString = queryString.substring(0, queryString.length() - 1); 
  writer.println("POST " + request.getRequestURL() + " " + queryString); 
 } 
}

以上代碼簡略易懂,願望對年夜家進修 java post get url 要求參數的相干辦法有所贊助,感激年夜家一向以來對網站的支撐,有你們的支撐,我們會做的更好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved