The
specific code looks like this:
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; }
Here is a description of how Java gets get and POST request URLs and parameter lists
Get requests in the servlet can obtain the complete request path and request all parameter lists through the HttpServletRequest Getrequesturl method and GetQueryString (), the need for post Getparametermap () method traversal, either get or post can be getrequesturl+getparametermap () to obtain the full path of the request
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, Ioexceptio
n {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 + "&"; }
}
// Remove the last space querystring = querystring.substring (0, Querystring.length ()-1);
Writer.println ("Get" + request.getrequesturl () + "" + querystring); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti
on {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 + "&";
}///Remove the last space querystring = querystring.substring (0, Querystring.length ()-1);
Writer.println ("POST" + request.getrequesturl () + "" + querystring); }
}
The above code is easy to understand, I hope to learn from the Java post get URL request parameters related methods to help, thank you all the time to the cloud-Habitat community website support, with your support, we will do better.