如题:谢谢各位

解决方案 »

  1.   

    httpRequest.addHeader("if-Modified-Since", feed.date);
      

  2.   

    private static synchronized void getHttpClient() {
    if (httpClient == null) {
    HttpParams httpparameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpparameters, 20000);// 设置连接超时
    HttpConnectionParams.setSoTimeout(httpparameters, 25000);// 设置请求超时
    HttpConnectionParams.setSocketBufferSize(httpparameters, 8192); //最大待待时间
    ConnManagerParams.setTimeout(httpparameters, 30000);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory
    .getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory
    .getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
    httpparameters, schReg);
    httpClient = new DefaultHttpClient(conMgr, httpparameters);
    }
    }getHttpClient();
    httpGet.setURI(new URI(strurl));
    HttpResponse response = httpClient.execute(httpGet);

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    ins = response.getEntity().getContent();
    }
    // url.openConnection().setDefaultUseCaches(true);
    Options options = new Options();
    options.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeStream(ins, null, options);
    int srcWidth = options.outWidth;
    ins.close();
    ins = null;
    options.inJustDecodeBounds = false;
    Log.d("Tag", "with:" + options.outWidth);
    // int be = (int) (((double) options.outWidth) / ((double)
    // displayWidth));
    int be = 0;
    be = (int) Math
    .round(((((double) srcWidth) / displayWidth))); options = new Options();
    options.inSampleSize = be;
    // ins = url.openStream(); response = httpClient.execute(httpGet);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    ins = response.getEntity().getContent();
    } bitmap = BitmapFactory.decodeStream(ins, null, options);
      

  3.   


     你这里有Http header 的信息??
      

  4.   

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;public class UrlHtml {
    /** 
         * 根据URL获得所有的html信息 
         * @param url 
         * @return 
         */  
        public static String getHtmlByUrl(String url){  
            String html = null;  
            HttpClient httpClient = new DefaultHttpClient();//创建httpClient对象  
     /*       HttpParams params = httpClient.getParams();  
            params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); */
            HttpGet httpget = new HttpGet(url);//以get方式请求该URL  
            //有的网站会先判别用户的请求是否是来自浏览器,如不是,则返回不正确的文本,所以用httpclient抓取信息时在头部加入如下信息,如http://www.oschina.net/网址就是这样处理才能抓
            httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)"); 
            //httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            try {  
                HttpResponse responce = httpClient.execute(httpget);//得到responce对象  
                int resStatu = responce.getStatusLine().getStatusCode();//返回码  
                if (resStatu==HttpStatus.SC_OK) {//200正常  其他就不对  
                    //获得相应实体  
                    HttpEntity entity = responce.getEntity();  
                    if (entity!=null) {  
                        html = EntityUtils.toString(entity);//获得html源代码  
                    }  
                }  
            } catch (Exception e) {  
                System.out.println("访问【"+url+"】出现异常!");  
                e.printStackTrace();  
            } finally {  
                httpClient.getConnectionManager().shutdown();  
            }  
            return html;  
        }  
    }本人用的是httpclient-4.2.5版本的httpclient,你去官网下这个版本的zip,解压把lib目录下的jar包导到工程就是了,上面提供的是一个抓取html的类,解析你自己用jsoup处理