2013-11-15 1 views
0

를 사용하여 나는이 PHP 코드를 다른 사이트로 나를 리디렉션 사이트가있을 때 리디렉션 :HttpClient를 POST 방법

<?php 
    header("Location: new.php?id=".$_POST["id"]."&test=".rand(5,15)); 
    echo "35"; 
?> 

-new.php

<?php 
    echo "ID: ".$_GET["id"]."| TEST: ".$_GET["test"]; 
?> 

을 내가 POST 요청을 보내려고하는 경우 HTTPClient를 사용하면 사이트가 다른 사이트로 리디렉션되지 않습니다 (게시물 요청의 응답은 35입니다). Get 요청을 보낼 때 완벽하게 작동합니다. 요청의 응답은 ID : | TEST : 13

-http.java

public class Http { 
    public static void main(String[] args) { 
     HttpResponse response; 
     CloseableHttpClient client = HttpClients.createDefault(); 

     HttpPost post = new HttpPost("http://localhost/test.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("id","55")); 
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      response = client.execute(post); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent())); 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
       System.out.println(line); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      HttpGet get = new HttpGet("http://localhost/test.php"); 
      response = client.execute(get); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent())); 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
       System.out.println(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 
} 

답변

2

자동으로 리디렉션 POST 요청은 RFC 표준에 위배됩니다. 따라서 HttpClient는 기본적으로이 작업을 수행하지 않습니다. 그러나 DefaultRedirectStrategy의 API에 따르면 LaxRedirectStrategy을 사용하면이 작업을 수행 할 수 있습니다.

DefaultHttpClient httpClient = new DefaultHttpClient(); 
    httpClient.setRedirectStrategy(new LaxRedirectStrategy()); 
    httpClient.execute(request); 
:

코드에서이 같이 보일 것이다

관련 문제