import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by ZhangZhengyi on 2016/3/4.
* <p>
*
*/
public class PostTest {
/**
* Get方式发起http请求
*/
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet get = new HttpGet("http://www.cnblogs.com/fengzanfeng/articles/1339347.html");
// get.setHeader();
System.out.println("Get 请求...." + get.getURI());
CloseableHttpResponse httpResponse = httpClient.execute(get);//执行请求
HttpEntity entity = httpResponse.getEntity();//得到结果的实体
InputStream content = entity.getContent();
if (null != entity) {
System.out.println(inputStream2String(content));
}
httpResponse.close();
if (httpClient != null) {
httpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String inputStream2String(InputStream in) throws IOException { //InputStream转化为String
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1; ) {
out.append(new String(b, 0, n));
}
return out.toString();
}
}
Get请求Demo
最新推荐文章于 2022-02-13 22:56:11 发布