-
Package com.stock.show.util;
-
-
Import Java.io.BufferedReader;
-
Import java.io.IOException;
-
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 Httprequestutil {
-
-
/**
-
* Request to send a GET method to the specified URL
-
*
-
* @param URL
-
* Send the requested URL
-
* @param param
-
* Request Parameters
-
* @return The response result of the remote resource represented by the URL
-
*/
-
public static string Sendget (string url, map<string,string> param) {
-
String result = "";
-
BufferedReader in = null;
-
try {
-
String urlnamestring = URL;
-
-
Add parameter
-
if (Param!=null&m.size () >0) {
-
urlnamestring = urlnamestring+ "?";
-
For (String Key:param.keySet ()) {
-
urlnamestring = urlnamestring + key + "=" + param.get (key);
-
}
-
}
-
-
URL realurl = new URL (urlnamestring);
-
Opening and linking between URLs
-
URLConnection connection = Realurl.openconnection ();
-
To set common request properties
-
Connection.setrequestproperty ("Accept", "*/*");
-
Connection.setrequestproperty ("Connection", "keep-alive");
-
Connection.setrequestproperty ("User-agent",
-
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
-
Establish an actual connection
-
Connection.connect ();
-
Get all response header fields
-
map<string, list<string>> map = Connection.getheaderfields ();
-
Iterate through all the response header fields
-
For (String Key:map.keySet ()) {
-
SYSTEM.OUT.PRINTLN (key + "--->" + map.get (key));
-
}
-
Defines the response of the BufferedReader input stream to read the URL
-
in = new BufferedReader (New InputStreamReader (
-
Connection.getinputstream ()));
-
String Line;
-
while (line = In.readline ()) = null) {
-
result + = line;
-
}
-
} catch (Exception e) {
-
SYSTEM.OUT.PRINTLN ("Send GET request exception!") "+ e);
-
E.printstacktrace ();
-
}
-
Use the finally block to close the input stream
-
finally {
-
try {
-
if (in = null) {
-
In.close ();
-
}
-
} catch (Exception E2) {
-
E2.printstacktrace ();
-
}
-
}
-
return result;
-
}
-
-
/**
-
* Request to send the Post method to the specified URL
-
*
-
* @param URL
-
* Send the requested URL
-
* @param param
-
* Request Parameters
-
* @return The response result of the remote resource represented
-
*/
-
public static string Sendpost (string url, map<string,string> param) {
-
PrintWriter out = null;
-
BufferedReader in = null;
-
String result = "";
-
try {
-
URL realurl = new URL (URL);
-
Opening and linking between URLs
-
URLConnection conn = Realurl.openconnection ();
-
To set common request properties
-
Conn.setrequestproperty ("Accept", "*/*");
-
Conn.setrequestproperty ("Connection", "keep-alive");
-
Conn.setrequestproperty ("User-agent",
-
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
-
To send a POST request, you must set the following two lines
-
Conn.setdooutput (TRUE);
-
Conn.setdoinput (TRUE);
-
Gets the output stream corresponding to the URLConnection object
-
out = new PrintWriter (Conn.getoutputstream ());
-
-
Processing parameters
-
String paramstring = "";
-
if (Param!=null&m.size () >0) {
-
For (String Key:param.keySet ()) {
-
paramstring = paramstring + key + "=" + param.get (key);
-
}
-
}
-
Send Request parameters
-
Out.print (paramstring);
-
Buffer for flush output stream
-
Out.flush ();
-
Defines the response of the BufferedReader input stream to read the URL
-
in = new BufferedReader (
-
New InputStreamReader (Conn.getinputstream ()));
-
String Line;
-
while (line = In.readline ()) = null) {
-
result + = line;
-
}
-
} catch (Exception e) {
-
SYSTEM.OUT.PRINTLN ("Send POST request exception!") "+e);
-
E.printstacktrace ();
-
}
-
Use the finally block to close the output stream, input stream
-
finally{
-
try{
-
if (out!=null) {
-
Out.close ();
-
}
-
if (in!=null) {
-
In.close ();
-
}
-
}
-
catch (IOException ex) {
-
Ex.printstacktrace ();
-
}
-
}
-
return result;
-
}
-
-
public static void Main (string[] args) {
-
Send a GET request
-
String s=httprequestutil.sendget ("http://hq.sinajs.cn/list=sh601006", null);
-
System.out.println (s);
-
-
-
}
-
-
}