코드 사본은 다음과 같습니다.
/**
* FileUtils가 지원하지 않기 때문에 Method String Content = 추가하십시오.
* fileUtils.ReadFileToString (fileUtils.tofile (신규
* url ( "http://www.baidu.com"));
*
* @param 소스
* @param 인코딩
* @반품
* @throws ioexception
*/
public static string readurltostring (url source)은 ioexception {
return readurltoString (소스, null);
}
/**
* FileUtils가 지원하지 않기 때문에 메소드를 추가하십시오.
*
* <fre>
* 문자열 content = fileUtils.ReadFileToString (fileUtils.tofile (new url)
* http://www.baidu.com)), GB2312);
* </pre>
*
* @param 소스
* @param 인코딩
* @반품
* @throws ioexception
*/
public static string readurltoString (URL 소스, 문자열 인코딩)
IoException {{
inputStream input = source.openstream ();
노력하다 {
return ioutils.tostring (입력, 인코딩);
} 마지막으로 {
ioutils.closequietly (입력);
}
}
/**
* URL의 내용을 읽으십시오 (메소드는 게시되며 여러 매개 변수를 지정할 수 있습니다).
* @param url
* @param 인코딩
* @param params 맵 매개 변수 (키는 매개 변수 이름, 값은 매개 변수 값)
* @return 문자열
* @throws ioexception
*/
public static string readurltoStringbypost (url url, String encoding, map <string, string> params)
IoException {{
httpurlconnection con = null;
// 요청 매개 변수 빌드
StringBuffer sb = new StringBuffer ();
if (params! = null) {
for (enther <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);
}
}
// 요청을 보내려고합니다
노력하다 {
con = (httpurlConnection) url.openConnection ();
con.setRequestMethod ( "post");
con.setdooutput (true);
con.setdoinput (true);
con.setUecaches (false);
con.setRequestProperty ( "content-type", "application/x-www-form-urlencoded");
outputStreamWriter OSW = 새로운 outputStreamWriter (con.getOutputStream (), 인코딩);
if (params! = null) {
osw.write (sb.tostring ());
}
osw.flush ();
osw.close ();
} catch (예외 e) {
logfactory.getLog (fileUtils.class) .error ( "post ("+url.toString ()+") 오류 ("+e.getMessage ()+")", e);
} 마지막으로 {
if (con! = null) {
con.disconnect ();
}
}
// 반환 내용을 읽습니다
StringBuffer buffer = new StringBuffer ();
노력하다 {
bufferedReader br = new bufferedReader (new inputStreamReader (con
.getInputStream (), 인코딩));
문자열 온도;
while ((temp = br.readline ())! = null) {
Buffer.Append (Temp);
Buffer.Append ( "/n");
}
} catch (예외 e) {
e.printstacktrace ();
}
return buffer.toString ();
}