wzh.Http Package;
Import java.io.BufferedReader;
Import java.io.IOException;
Import the java.io.InputStreamReader;
Import java.io.PrintWriter;
Import the java.net.URL;
Import the java.net.URLConnection;
Import Java. util.List;
Import a java.util.Map;
public class the HttpRequest {
/ **
* GET method request transmission to the specified the URL
*
* @param URL
* transmission request the URL
* @param param
* request parameters, the request parameter should be name1 = value1 & name2 = value2 form of.
* Represents the remote resource @return URL response result
* /
public static String sendGet (URL String, String param) {
String Result = "";
= Null in BufferedReader;
the try {
"?" UrlNameString String url + = + param;
the URL of the URL of realUrl = new new (urlNameString);
connection between // Open and the URL of
URLConnection Connection = realUrl.openConnection ();
// set general request attribute
connection.setRequestProperty ( "Accept", "* / *");
connection.setRequestProperty ( "Connection", "the Keep-Alive");
connection.setRequestProperty ( "User-Agent",
"the Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
// establish the actual connection
connection.connect ();
// get all response header field
Map <String, List <String >> connection.getHeaderFields Map = ();
// traverse all the response header fields
for (String Key: map.keySet ()) {
System.out.println (Key + "---> "+ as map.get (Key));
}
// read the input stream defined BufferedReader URL response
in = new new BufferedReader (new new the InputStreamReader (
connection.getInputStream ()));
String Line;
the while ((Line = in.readLine ! ()) = null) {
Result = Line +;
}
} the catch (exception E) {
System.out.println ( "GET request transmits abnormal!" + E);
e.printStackTrace ();
}
// Use finally block to close the input stream
finally {
the try {
IF (= null in!) {
In.close ();
}
} the catch (Exception E2) {
e2.printStackTrace ();
}
}
return Result;
}
/ **
* POST request to the designated method of the URL
*
* @param URL
* transmission request the URL
* @param param
* request parameters, the request parameter should be in the form name1 = value1 & name2 = value2 of.
* @Return represents remote resource response results
* /
public static String sendPost (URL String, String param) {
OUT = null the PrintWriter;
the BufferedReader in = null;
String Result = "";
the try {
the URL = new new realUrl the URL (URL);
the connection between the opening and // the URL
the URLConnection realUrl.openConnection Conn = ();
// set general request attribute
conn.setRequestProperty ( "Accept", "* / *");
conn.setRequestProperty ( "Connection", "the Keep-Alive");
conn.setRequestProperty ( "User-Agent",
"the Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
// send a POST request must set the following two lines
conn.setDoOutput (to true);
conn.setDoInput (to true);
// Get URLConnection object corresponding to the output stream
OUT = the PrintWriter new new (conn.getOutputStream ());
// send a request parameter
Out.print (param);
// the flush buffered output stream
out.flush ();
// define BufferedReader the input stream is read in response to the URL
in the BufferedReader new new = (
new new the InputStreamReader (conn.getInputStream ()));
String Line;
the while (! (= in.readLine Line ()) = null) {
Result = Line +;
}
} the catch (exception E) {
System.out.println ( "POST request transmission abnormality!" + E);
e.printStackTrace ();
}
// Use finally block to close the output stream, the input stream
finally {
the try {
IF (OUT = null!) {
The out.close ();
}
IF (= null in!) {
In.close ();
}
}
the catch (IOException EX) {
ex.printStackTrace ();
}
}
return Result;
}
Import java.io.BufferedReader;
Import java.io.IOException;
Import the java.io.InputStreamReader;
Import java.io.PrintWriter;
Import the java.net.URL;
Import the java.net.URLConnection;
Import Java. util.List;
Import a java.util.Map;
public class the HttpRequest {
/ **
* GET method request transmission to the specified the URL
*
* @param URL
* transmission request the URL
* @param param
* request parameters, the request parameter should be name1 = value1 & name2 = value2 form of.
* Represents the remote resource @return URL response result
* /
public static String sendGet (URL String, String param) {
String Result = "";
= Null in BufferedReader;
the try {
"?" UrlNameString String url + = + param;
the URL of the URL of realUrl = new new (urlNameString);
connection between // Open and the URL of
URLConnection Connection = realUrl.openConnection ();
// set general request attribute
connection.setRequestProperty ( "Accept", "* / *");
connection.setRequestProperty ( "Connection", "the Keep-Alive");
connection.setRequestProperty ( "User-Agent",
"the Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
// establish the actual connection
connection.connect ();
// get all response header field
Map <String, List <String >> connection.getHeaderFields Map = ();
// traverse all the response header fields
for (String Key: map.keySet ()) {
System.out.println (Key + "---> "+ as map.get (Key));
}
// read the input stream defined BufferedReader URL response
in = new new BufferedReader (new new the InputStreamReader (
connection.getInputStream ()));
String Line;
the while ((Line = in.readLine ! ()) = null) {
Result = Line +;
}
} the catch (exception E) {
System.out.println ( "GET request transmits abnormal!" + E);
e.printStackTrace ();
}
// Use finally block to close the input stream
finally {
the try {
IF (= null in!) {
In.close ();
}
} the catch (Exception E2) {
e2.printStackTrace ();
}
}
return Result;
}
/ **
* POST request to the designated method of the URL
*
* @param URL
* transmission request the URL
* @param param
* request parameters, the request parameter should be in the form name1 = value1 & name2 = value2 of.
* @Return represents remote resource response results
* /
public static String sendPost (URL String, String param) {
OUT = null the PrintWriter;
the BufferedReader in = null;
String Result = "";
the try {
the URL = new new realUrl the URL (URL);
the connection between the opening and // the URL
the URLConnection realUrl.openConnection Conn = ();
// set general request attribute
conn.setRequestProperty ( "Accept", "* / *");
conn.setRequestProperty ( "Connection", "the Keep-Alive");
conn.setRequestProperty ( "User-Agent",
"the Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
// send a POST request must set the following two lines
conn.setDoOutput (to true);
conn.setDoInput (to true);
// Get URLConnection object corresponding to the output stream
OUT = the PrintWriter new new (conn.getOutputStream ());
// send a request parameter
Out.print (param);
// the flush buffered output stream
out.flush ();
// define BufferedReader the input stream is read in response to the URL
in the BufferedReader new new = (
new new the InputStreamReader (conn.getInputStream ()));
String Line;
the while (! (= in.readLine Line ()) = null) {
Result = Line +;
}
} the catch (exception E) {
System.out.println ( "POST request transmission abnormality!" + E);
e.printStackTrace ();
}
// Use finally block to close the output stream, the input stream
finally {
the try {
IF (OUT = null!) {
The out.close ();
}
IF (= null in!) {
In.close ();
}
}
the catch (IOException EX) {
ex.printStackTrace ();
}
}
return Result;
}
}
Reprinted from: https: //www.cnblogs.com/guoyinli/p/7192839.html