HttpUtil
Posted Sun_TTTT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpUtil相关的知识,希望对你有一定的参考价值。
public class HttpUtilpublic static String sendHttpRequest(String url)
BufferedReader reader = null;
String result = null;
String charset = "utf-8";
StringBuffer sbf = new StringBuffer();
String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟浏览器
try
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection)url1.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(30000);
connection.setConnectTimeout(30000);
connection.setRequestProperty("User-agent",userAgent);
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(
is, charset));
String strRead = null;
while ((strRead = reader.readLine()) != null)
sbf.append(strRead);
sbf.append("\\r\\n");
reader.close();
result = sbf.toString();
catch (Exception e)
e.printStackTrace();
return result;
以上是关于HttpUtil的主要内容,如果未能解决你的问题,请参考以下文章