/**
* Because Fileutils is not supported, add a method String content =
* Fileutils.readfiletostring (Fileutils.tofile) (new
* URL ("http://www.baidu.com"));
*
* @param source
* @param encoding
* @return
* @throws IOException
*/
public static String readurltostring (URL source) throws IOException {
Return readurltostring (Source,null);
}
/**
* Because Fileutils is not supported, add a method
*
* <pre>
* String content = fileutils.readfiletostring (fileutils.tofile) (New URL (
* "http://www.baidu.com")), "gb2312");
* </pre>
*
* @param source
* @param encoding
* @return
* @throws IOException
*/
public static string readurltostring (URL source, string encoding)
Throws IOException {
InputStream input = Source.openstream ();
try {
return ioutils.tostring (input, encoding);
finally {
ioutils.closequietly (input);
}
}
/**
* Read the content of the URL (method is post, can specify multiple parameters)
* @param URL
* @param encoding
* @param params map parameters (key is parameter name, value is argument value)
* @return String
* @throws IOException
*/
public static string readurltostringbypost (URL url, String encoding,map<string, string> params)
Throws IOException {
HttpURLConnection con = null;
Build Request Parameters
StringBuffer sb = new StringBuffer ();
if (params!= null) {
For (entry<string, string> e:params.entryset ()) {
Sb.append (E.getkey ());
Sb.append ("=");
Sb.append (E.getvalue ());
Sb.append ("&");
}
if (Sb.length () >0) {
Sb.substring (0, Sb.length ()-1);
}
}
Attempt to send request
try {
Con = (httpurlconnection) url.openconnection ();
Con.setrequestmethod ("POST");
Con.setdooutput (TRUE);
Con.setdoinput (TRUE);
Con.setusecaches (FALSE);
Con.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
OutputStreamWriter OSW = new OutputStreamWriter (Con.getoutputstream (), encoding);
if (params!= null) {
Osw.write (Sb.tostring ());
}
Osw.flush ();
Osw.close ();
catch (Exception e) {
Logfactory.getlog (Fileutils.class). Error ("POST" ("+url.tostring () +") Error ("+e.getmessage () +"), e);
finally {
if (con!= null) {
Con.disconnect ();
}
}
Read return content
StringBuffer buffer = new StringBuffer ();
try {
BufferedReader br = new BufferedReader (New InputStreamReader (con
. getInputStream (), encoding));
String temp;
while (temp = Br.readline ())!= null) {
Buffer.append (temp);
Buffer.append ("\ n");
}
catch (Exception e) {
E.printstacktrace ();
}
return buffer.tostring ();
}