HttpClient 怎么获取连接的内容长度?? response.getEntity().getContentLength() 拿出来的是-1,高手指

如题所述

你看看
public void clientPost()
{
String httpUrl="http://10.0.2.2:8080/JDemo/android/httpget2.jsp";
// HttpPost连接对象
HttpPost httpRequest=new HttpPost(httpUrl);
//使用NameValuePair来保存要传递的Post参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
//添加要传递的参数
params.add(new BasicNameValuePair("par","HTTP_Client_android_Post"));
try {
//设置字符集
HttpEntity httpentity=new UrlEncodedFormEntity(params,"gb2312");
//请求httpRequest
httpRequest.setEntity(httpentity);
//取得HttpClient对象
HttpClient httpclient=new DefaultHttpClient();
//请求HttpCLient,取得HttpResponse
HttpResponse httpResponse=httpclient.execute(httpRequest);
//请求成功
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
{
//取得返回的字符串
String strResult=EntityUtils.toString(httpResponse.getEntity());
text1.setText(strResult.trim());
}else
{
text1.setText("请求错误!");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e)
{
text1.setText(e.getMessage().toString());
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-29
下面是用来设置 cookie 脚本的一个普通实例。
Set-Cookie: name = VALUE;
expires = DATE;
path = PATH;
domain = DOMAIN_NAME;

下面是COOKIE存储在浏览器端的一个实例,希望你能从中学习到有用的东西

用户每发起一次新的请求,浏览器在检查完本地存储 Cookie 的有效性后,会把所有由 MTS 产生的有效 Cookie 附加在请求头里送到 MTS。MTS 接受到客户端的翻译请求后,从 Request 中提取出所有的 Cookie,还原后根据目标服务器的 domain 和 path 进行过滤。产生所有与目标服务器相关的 Cookie。
// 从 request 中获取所有的 Cookie
javax.servlet.http.Cookie[] theCookies = request.getCookies();
ArrayList cookiesList = new ArrayList();
String url = request.getParameter("url");
String domain = URLUtil.getURLHost(url);
String path = URLUtil.getPath(url);
if (theCookies != null)
{
for (int i = 0; i < theCookies.length; i++)
{
RE r = new RE();
// 用正则表达式把 name 项还原成 domain,path,name
REDebugCompiler compiler = new REDebugCompiler();
r.setProgram(compiler.compile("\\|\\|"));
String[] values = r.split(theCookies[i].getName());
//"9.181.116.183||/MTModule||testCookie:value1" or " ||
||testCookie:value1"
if (values.length == 3)
{
if (values[0].trim().startsWith("."))
{
if (!domain.endsWith(values[0].trim()))
continue;
} else if (!domain.endsWith("://" + values[0].trim()))
continue;
if (!path.startsWith(values[1].trim()))
continue;
Cookie tempCookie = new Cookie();
tempCookie.setDomain(
("".equals(values[0].trim())) ? null : values[0]);
tempCookie.setPath(
("".equals(values[1].trim())) ? null : values[1]);
tempCookie.setName(
("".equals(values[2].trim())) ? null : values[2]);
tempCookie.setSecure(theCookies[i].getSecure());
tempCookie.setValue(theCookies[i].getValue());
tempCookie.setVersion(theCookies[i].getVersion());
tempCookie.setComment(theCookies[i].getComment());
cookiesList.add(tempCookie);
}
}
}
//transferedCookie 用来存储将被传到目标服务器的 Cookie
Cookie[] transferedCookie = new Cookie[cookiesList.size()];
cookiesList.toArray(transferedCookie);

接下来,需要把 Cookie 送到目标服务器中。我们使用 HTTPClient 与目标服务器连接。HTTPClient 在与目标服务器连接以后,允许服务器设置 Cookie 并在需要的时候自动将 Cookie 返回服务器,也支持手工设置 Cookie 后发送到服务器端。但是,由于如何处理 cookie 有几个规范互相冲突:Netscape Cookie 草案、RFC2109、RFC2965,而且还有很大数量的软件商的 Cookie 实现不遵循任何规范。 为了处理这种状况,需要把 HttpClient 设置成 Cookie 兼容模式,这样可以最大限度的处理好各种 Cookie。下面的代码把 Cookie 送到目标服务器。

HttpClient client = new HttpClient();
// 从 request 得到所有需要传输的 cookie
Cookie[] questCookie = getCookieFromRequest(request);
// 设置 HTTPClient 为 Cookie 兼容模式
client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
if (questCookie.length > 0)
// 把 Cookie 加到 httpclient 中
client.getState().addCookies(questCookie);
HttpMethod method = new GetMethod(TagerURL);
// 向目标服务器发送请求
int statusCode = client.executeMethod(method);
method.releaseConnection();

MTS 把请求和 Cookie 送出后,继续接收目标服务器的应答,读取返回的原始 Cookie,并转换成可以存储在用户浏览器端的 Cookie。下面的代码将对原始 Cookie 的内容进行变换,保留 expires 和 secure 等项,把 domain 和 path 项编码到 name 中去。

// 从 HTTPClient 中取得所有的 Cookie
Cookie[] temp = client.getState().getCookies();
if (temp != null)
{
javax.servlet.httpCookie theCookie = new javax.servlet.http.Cookie[temp.length];
// 逐一对 Cookie 进行处理
for (int i = 0; i < temp.length; i++)
{ StringBuffer sb = new StringBuffer();
// 编码成 domain||path||name
sb.append(
temp[i].getDomain() == null ? " " : temp[i].getDomain());
sb.append("||");
sb.append(temp[i].getPath() == null ? " " : temp[i].getPath());
sb.append("||");
sb.append(temp[i].getName() == null ? " " : temp[i].getName());
theCookie[i] =
new Cookie(sb.toString(),temp[i].getValue());
// 复制其他项
theCookie[i].setMaxAge(theCookie[i].getMaxAge();
theCookie[i].setSecure(temp[i].getSecure());
theCookie[i].setVersion(temp[i].getVersion());
theCookie[i].setComment(temp[i].getComment());
}
}

最后一步,把这些 Cookie 保存到 response 里,随 HTTP 应答头返回用户浏览器。并保存在浏览器中。

// 把所有转换后的 Cookie 加入 response
for (int i = 0; i < theCookie.length; i++) {
response.addCookie(theCookie[i]);
}

至此,我们已经完成了接收用户请求,转换 Cookie,发送到目标服务器,接收目标服务器的原始 Cookie,并保存在客户浏览器的整个处理过程。本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网