2013-04-24 2 views
0

HTTPClient 4.2.2를 사용하여 HTTP POST 요청을하고 있습니다. 게시 요청에서 언급 한 URL에 액세스하려면 .pfx 인증서를 사용하고 있습니다. 하지만 302를 얻고, 일시적으로 이동오류 : HTTP/1.1 302 Moved Temporary

// 자바 코드

public class CertificateAuth { 

    private static final long TIMEOUT = 500000000L; 

    //set trust store to be used to trust server certificate 

    private String tokeApiPostUrl = "http://test.com/l1/rest1/lt/v1/data"; 
    private String tokenPost = "{\"id\": \"Token_15555\",\"type\": \"token\",\"entity_type\": \"Store\",\"entity_id\": \"StoreId\",\"expiration_time\": 1376579410}"; 


    //client is taken as class varibable so that Cookies set by Server persists between 
    //multiple calls 
    private HttpClient client = null; 


    public CertificateAuth() { 

    } 


    public String createToken() throws Exception { 

     // set reasonable timeouts as we seem to wait forever to get a response: 

     KeyStore keystore = KeyStore.getInstance("pkcs12"); 
     InputStream keystoreInput = new FileInputStream("abc.pfx"); 
     keystore.load(keystoreInput, "password".toCharArray()); 

     SchemeRegistry schemeRegistry = new SchemeRegistry(); 
     SSLSocketFactory lSchemeSocketFactory = new SSLSocketFactory(keystore, "qwerty10"); 
     schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
     schemeRegistry.register(new Scheme("https", 443, lSchemeSocketFactory)); 
     final HttpParams httpParams = new BasicHttpParams(); 
     client = new DefaultHttpClient(new SingleClientConnManager(httpParams, schemeRegistry), httpParams); 
     String version = null; 
     HttpPost httpPost = new HttpPost(tokeApiPostUrl); 
    // httpPost.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE); 
     client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 

     try { 
      Map<String, String> headersParameters = new HashMap<String, String>();   
      JSONObject jsonObj = new JSONObject(tokenPost); 
      setParametersJson(httpPost, headersParameters, jsonObj); 
      HttpResponse resp = client.execute(httpPost); 
      if(resp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK || resp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_CREATED) { 
       System.out.println("Succesfully queried"); 
      } 
     } finally { 
      httpPost.releaseConnection(); 
     } 
     return version; 
    } 

    private void setParametersJson(HttpRequestBase httpOperation, Map <String, String> headerParameters, JSONObject jsonObject) { 
     for (String headerName : headerParameters.keySet()) { 
      httpOperation.setHeader(headerName, headerParameters.get(headerName)); 
     } 
     if (jsonObject != null) { 
      try { 
       StringEntity stringEntity = new StringEntity(jsonObject.toString()); 

       if (httpOperation instanceof HttpPost) { 
        ((HttpPost) httpOperation).setEntity(stringEntity); 
       } else if (httpOperation instanceof HttpPut) { 
        ((HttpPut) httpOperation).setEntity(stringEntity); 
       } 
      } catch(UnsupportedEncodingException ex) { 
       ex.printStackTrace(); 
      } catch(Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 


    public static void main(String[] args) throws Exception { 
     CertificateAuth ua = new CertificateAuth(); 
     ua.createToken(); 

    } 
} 

답변

1

이 코드이 줄을 추가 오류가 발생했습니다.

client.setRedirectStrategy(new LaxRedirectStrategy()); 
+0

작동하지 않습니다. – user2314126

+0

라인을 추가합니다. client.setRedirectStrategy (new LaxRedirectStrategy()); 그러나 요청을 게시하는 것이 아닙니다. – user2314126

+2

위의 코드는 HttpGet에서 작동하지만 HttpPost 및 HttpPut에서 302 오류가 발생합니다. – user2314126