Android中使用httpclient访问服务器,需要session功能

本文提供了一个Android平台上的网络请求示例,展示了如何使用HttpURLConnection发起GET请求获取网页内容,并通过InputStream读取返回的数据。
虽然只有十分
  但是还是给你写一段   android中测试  跑得了  加点分可以继续追问我哦 呵呵~~~~
  public static  String getHtml( String urlpath,String encoding) throws Exception {
  URL url = new URL(urlpath);
  //实例化一个HTTP连接对象conn
  HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  //定义请求方式为GET,其中GET的格式需要注意
  conn.setRequestMethod("GET");
  //定义请求时间,在ANDROID中最好是不好超过10秒。否则将被系统回收。
  conn.setConnectTimeout(6*1000);
  if(conn.getResponseCode()== 200){
  InputStream inStream = conn.getInputStream();
  byte[] data = readStream(inStream);
  return new String(data,encoding);

  }
  return null;
  }
  public static  byte[] readStream(InputStream inStream) throws Exception{
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len= -1;
  //将输入流不断的读,并放到缓冲区中去。直到读完
  while((len=inStream.read(buffer))!=-1){
  //将缓冲区的数据不断的写到内存中去。
  outStream.write(buffer, 0, len);
  }
  outStream.close();
  inStream.close();
  return outStream.toByteArray();
  }
14
| 评论(8)

转载于:https://my.oschina.net/limbusnet/blog/86325

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值