欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java如何实现URL带请求参数(get/post)及得到get和post请求url和参数列表的方法

程序员文章站 2024-03-07 15:15:21
具体代码如下所示: public static string sendget(string url,string param) { string resu...

具体代码如下所示:

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 请求参数的相关方法有所帮助,感谢大家一直以来对网站的支持,有你们的支持,我们会做的更好。