fileutils extension readurltostring read URL content _java

Source: Internet
Author: User
Tags stringbuffer

Copy Code code as follows:

/**
* 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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.