当前位置: 代码迷 >> J2EE >> HttpClient4 重定向有关问题
  详细解决方案

HttpClient4 重定向有关问题

热度:237   发布时间:2016-04-22 00:56:33.0
HttpClient4 重定向问题
Java code
private static HttpClient client = new DefaultHttpClient();    static void login(String txtID,String txtPW) throws IOException {    static void login(String txtID,String txtPW) throws IOException {//        HttpPost post = new HttpPost("http://www.yojj.com");          HttpGet get1 =  new HttpGet("http://www.yojj.com/login_code.aspx?1");          HttpResponse res = client.execute(get1);          File f = new File("d:\\test454ss5.bmp");          OutputStream ops = new FileOutputStream(f);          res.getEntity().writeTo(ops);        String imgStr = new GetIbcbetImg().getImg(f);         ops.close();         String pw = MD5Util.MD5(txtPW+imgStr);         EntityUtils.consume(res.getEntity());        HttpGet get =  new HttpGet("http://www.yojj.com/ProcessLogin.aspx?"                +"selLang=en&txtID="+txtID+"&txtPW="+pw+"&txtCode="                +imgStr+"&hidKey=&hidLowerCasePW=&hidServerKey=ibcbet.com");        get.setHeader("Host", "www.yojj.com");        get.setHeader("User-Agent",                "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");        get.setHeader("Accept",                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");        get.setHeader("Accept-Language", "zh-CN,zh;q=0.8");        get.setHeader("Accept-Encoding", "gzip,deflate,sdch");        get.setHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");        get.setHeader("Connection", "keep-alive");        get.setHeader("Referer", "http://www.yojj.com/");        //HttpResponse response = client.execute(get);        System.out.println(res.getStatusLine().getStatusCode());        res = client.execute(get);        HttpEntity entity = res.getEntity();        String result = dumpRes(entity);        if(result.indexOf("www.yojj.com:80/rulesalert.aspx") > 0){            HttpGet get4 = new HttpGet("http://www.yojj.com/rulesalert.aspx?Accept=YES");            HttpResponse response4 = client.execute(get4);            HttpEntity entity4 = response4.getEntity();            System.out.println(response4.getStatusLine().getStatusCode());            System.out.println(EntityUtils.toString(entity4, "UTF-8"));                        HttpGet get6 = new HttpGet("http://ytojh.yojj.com/");            HttpResponse response6 = client.execute(get6);               int statusCode6 = response6.getStatusLine().getStatusCode();                                    String url = "http://ytojh.yojj.com/ValidateTicket.aspx?c=%2fQGFDRdYp0k%3d&t=1745925191&l=en&f=nReSaj6pl4g%3d&o=16GPeAjqfVU5kfIPYgw1Tg%3d%3d";            HttpGet get5 = new HttpGet(url);            HttpResponse response5 = client.execute(get5);//[color=#FF0000]地址变了这里错误[/color]               int statusCode = response5.getStatusLine().getStatusCode();//[color=#FF0000]网上都说这里302的时候重定向但是我上面一行就错了还没到这里[/color]        }            }
这个怎么做啊 可以加分啊

------解决方案--------------------
你有福气了,这应该是个Bug:
https://issues.apache.org/jira/browse/HTTPCLIENT-1193


一个可能的临时解决方案是使用SingleClientConnManager或者PoolingClientConnectionManager,类似于:
ClientConnectionManager connManager = new PoolingClientConnectionManager();
DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
------解决方案--------------------
HttpGet get6 = new HttpGet("http://ytojh.yojj.com/");
HttpResponse response6 = client.execute(get6);
这里网址换了,client不要用原来的了,再创建一个,用新的。