コードコピーは次のとおりです。
/**
* fileutilsはそれをサポートしていないため、メソッド文字列content =を追加します
* fileutils.readfiletostring(fileutils.tofile(new
* url( "http://www.baidu.com")));
*
* @paramソース
* @paramエンコーディング
* @戻る
* @throws ioexception
*/
public static string readurltostring(url source)はioexceptionをスローします{
Readurltostring(ソース、null)を返します。
}
/**
* fileutilsはそれをサポートしていないため、メソッドを追加します
*
* <pre>
* string content = fileutils.readfiletostring(fileutils.tofile(new URL(
* http://www.baidu.com))、gb2312);
* </pre>
*
* @paramソース
* @paramエンコーディング
* @戻る
* @throws ioexception
*/
public static string readurltostring(url source、string ending)
IOExceptionをスローする{
inputstream input = source.openstream();
試す {
ioutils.toString(入力、エンコード)を返します。
} ついに {
ioutils.closequetly(input);
}
}
/**
* URLのコンテンツを読む(メソッドはpostで、複数のパラメーターを指定できます)
* @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(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);
}
}
//リクエストを送信してみてください
試す {
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()、エンコード);
if(params!= null){
osw.write(sb.tostring());
}
osw.flush();
osw.close();
} catch(例外e){
logfactory.getLog(fileutils.class).error( "post("+url.toString()+")error("+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();
}