GET方式获取服务器数据---利用apache的jar包
2015-08-14 14:50
615 查看
<span style="font-size:18px;">import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; //使用apache提供的jar包读取服务器返回的数据 public class HttpUtils2 { public static void main(String[] args) throws ClientProtocolException, IOException { String path = "http://localhost:8080/day01"; InputStream in = HttpUtils2.getInputStream(path); System.out.println(HttpUtils2.getResult(in, "utf-8")); } public static InputStream getInputStream(String path) throws ClientProtocolException, IOException { HttpGet get = new HttpGet(path); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(get); InputStream in=null; if(response.getStatusLine().getStatusCode()==200) { in = response.getEntity().getContent(); } return in; } public static String getResult(InputStream in,String code) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b= new byte[1024]; int len=0; while((len=in.read(b))!=-1) { baos.write(b, 0, len); } in.close(); return new String(baos.toByteArray(),code); } }</span>
相关文章推荐
- post模拟表单数据提交--利用apache的jar包
- 利用apache来做集群,实现负载均衡
- [转载] Apache Lucene初探
- Apache端口复用-模块rootme
- Apache的压力测试工具ab
- Python开发入门与实战16-APACHE部署
- 如何编译Apache Hadoop2.6.0源代码
- apache activemq 学习笔记
- org.apache.commons.lang3功能示例
- 优化Apache,提高并发
- Apache2 Axis2/C 搭建 hello world
- Apache配置文件详解
- Amazon Linux 编译安装 Apache 2.4 + PHP 5.5
- org.apache.hadoop.hdfs.server.datanode.DataNode: Exception in BPOfferService for Block pool
- 服务器配置apache2.4+php5.6+mysql
- Spark Shuffle过程的一些小结(译 Shuffle Performance in Apache Spark)
- Linux+Apache+DB2+PHP环境配置
- django apache 发布
- No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.mave
- Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of