2013-07-20 2 views
2

HTTPClient()로 웹 사이트에 로그인하고 싶습니다. 나는 리다이렉트를 따라갈 수 없다 (나는 예외가있다).HTTPCLIENT : isRedirectNeeded

public String getPage(HttpClient myClient, PostMethod myMethod, HostConfiguration hostConfig) { 

      [...] 

      try { 
       // execute methode 
       int returnCode = myClient.executeMethod(hostConfig, myMethod); 


       // handle redirection 
       if (returnCode == HttpStatus.SC_MOVED_TEMPORARILY 
         || returnCode == HttpStatus.SC_MOVED_PERMANENTLY 
         || returnCode == HttpStatus.SC_SEE_OTHER 
         || returnCode == HttpStatus.SC_TEMPORARY_REDIRECT) { 

        System.out.println("Redirection encountered:" + returnCode); 

        String redirectLocation = null; 
        Header locationHeader = myMethod.getResponseHeader("location"); 
        if (locationHeader != null) { 
         redirectLocation = locationHeader.getValue(); 

        } else { 
         System.err.println("ERROR : 01"); 
        } 

        if (redirectLocation != null) { 
         System.out.println("redirectLocation : "+redirectLocation); 
         hostConfig.setHost(new URI(url + redirectLocation, true)); 
         GetMethod redirect = new GetMethod(url + redirectLocation); 
       returnCode = myClient.executeMethod(hostConfig, redirect); 
        } 

       } 
       // Handle redirects 

       else if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { 
        [...] 
       } else { 
          // get Result in String 
        } 
       } 
      } catch (Exception e) { 
       System.err.println(e); 
      } finally { 
       myMethod.releaseConnection(); 
       if (br != null) { 
        try { 
         br.close(); 
        } catch (Exception fe) { 
        } 
       } 
      } 

      return retval; 

     } 

그리고 내 껍질 결과 :

client = new HttpClient(); 
     client.getParams().setParameter(HttpMethodParams.USER_AGENT, useragent); 
     client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); 
     client.getParams().setParameter("http.protocol.allow-circular-redirects", true); 
     client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); 

[...] 

HostConfiguration hostConfig = client.getHostConfiguration(); 
       hostConfig.setHost(new URI(url, true)); 

       PostMethod post = new PostMethod(login); 
       post.addParameter("username", pseudo); 
       post.addParameter("password", password); 
       post.addParameter("secure_login", tooken); 

getPage(client, post, hostConfig); 

그리고 내 GetPage 방법에

, 나는 다음과 리디렉션을 실행

나는 나의 클라이언트와 hostconfig에를 만들

Redirection encountered:302 
redirectLocation : /?section=INDEX 
juil. 20, 2013 12:16:07 PM org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded 
INFO: Redirect requested but followRedirects is disabled 

내가 어떻게 리디렉션을 추적 할 수 있습니까?

PS : 내가 사용하는 경우 :

myMethod.setFollowRedirects(true); 

내가 다른 예외가 : 내 질문부터 응답을하지 않았기 때문에 최대

java.lang.IllegalArgumentException: Entity enclosing requests cannot be redirected without user intervention 
+0

[HTTP : // 유래 .com/questions/3091053/how-to-login-in-web-site-using-java] [1] 위의 질문에 대한 답변을 확인하십시오. uestion. 그것은 당신을 도울 수 있습니다. [1] : http://stackoverflow.com/questions/3091053/how-to-login-in-web-site-using-java –

답변

0

내가 항목을 어제

+0

이러한 종류의 정보를 보려면 주석 기능을 사용해보십시오. – reto

+0

죄송합니다, 저는 Stack Overflow 초보자입니다 – TheorY

+0

다시 화제가되었습니다 – TheorY