日期:2014-05-17  浏览次数:20634 次

用HttpClient请求url对应的网页数据,请求不到,求高手解答,在线等
请求的url是:http://list.tmall.com/search_product.htm?spm=3.1000473.254665.12&active=1&from=sn_1_cat&area_code=330100&navlog=1&nav=spu-cat&vmarket=0&style=g&sort=s&start_price=100&n=120&s=0&cat=50096891&is=cate#J_crumbs
写的代码如下:
Java code

                // 传进去上面的url
                HttpClient client = new DefaultHttpClient();
        try {
            HttpPost httpPost = new HttpPost(url.trim());
            HttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity == null) {
                System.out.println("url content is null:" + url);
            }
            System.out.println(EntityUtils.toString(entity);)
        } catch (Exception e) {
            e.printStackTrace();
        }


输出为空
但是输入别的网址,如www.baidu.com就可以请求到
这个问题如何解决?

------解决方案--------------------
你这地址是教程的吗?怎么是天猫网站的地址

------解决方案--------------------
有些网站用程序代开的时候需要设置对应的头文件,你可以到网上查一下相关资料,也有的时候是因为你请求的页面是需要登录的,你再琢磨琢磨吧
------解决方案--------------------
通过 PostMethod postMethod = new PostMethod();

httpClient.executeMethod(postMethod);

InputStream is = postMethod.getResponseBodyAsStream();

读取流的方式,把得到的流转成字符串输出吧。

这个网址response.getEntity();返回是空。
用下面这个输出看一下就知道了。
System.out.println("Response content length: " + entity.getContentLength());