
在Android中使用httpclient访问服务器,需要session功能,求代码!
1个回答
展开全部
虽然只有十分
但是还是给你写一段 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();
}
但是还是给你写一段 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();
}

2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询