2011-01-15 2 views
3

execute(HttpPost post) 메서드를 사용하여 Apache의 DefaultHttpClient()을 사용하여 http POST를 수행합니다. 이 웹 사이트에 로그온합니다. 그런 다음 동일한 클라이언트를 사용하여 HttpGet을 만들고 싶습니다. 그러나 내가 할 때, 나는 예외 얻을 : 스레드 "주요"java.lang.IllegalStateException에서POST 후 HttpClient를 사용하여 GET을 실행할 때 예외가 발생했습니다.

예외 : SingleClientConnManager의 잘못된 사용 : 아직 할당 연결합니다.

왜 이런 일이 발생하는지 확신 할 수 없습니다. 어떤 도움을 주시면 감사하겠습니다.

public static void main(String[] args) throws Exception { 

    // prepare post method 
    HttpPost post = new HttpPost("http://epaper02.niedersachsen.com/epaper/index_GT_neu.html"); 

    // add parameters to the post method 
    List <NameValuePair> parameters = new ArrayList <NameValuePair>(); 
    parameters.add(new BasicNameValuePair("username", "test")); 
    parameters.add(new BasicNameValuePair("passwort", "test")); 

    UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8); 
    post.setEntity(sendentity); 

    // create the client and execute the post method 
    HttpClient client = new DefaultHttpClient(); 
    HttpResponse postResponse = client.execute(post); 
    //Use same client to make GET (This is where exception occurs) 
    HttpGet httpget = new HttpGet(PDF_URL); 
    HttpContext context = new BasicHttpContext(); 

    HttpResponse getResponse = client.execute(httpget, context); 



    // retrieve the output and display it in console 
    System.out.print(convertInputStreamToString(postResponse.getEntity().getContent())); 
    client.getConnectionManager().shutdown(); 


} 
+0

참조 : http://stackoverflow.com/questions/4612573/exception-using-httprequest-execute-invalid-use-of-singleclientconnmanager-co –

답변

2

POST 이후에는 연결 관리자가 여전히 POST 응답 연결을 유지하고 있기 때문입니다. 클라이언트를 다른 용도로 사용하려면 먼저 해당 버전을 릴리스해야합니다.

이 작동합니다 :

HttpResponse postResponse = client.execute(post); 
EntityUtils.consume(postResponse.getEntity(); 

그런 다음, 당신이 당신의 GET을 실행할 수 있습니다.

+0

고마워요! 그것은 예외를 위해 그것을했다! – tzippy

+1

EntityUtils.consume이란 무엇입니까? 해당 방법을 해결할 수 없습니다. –