0

HTTPClient 4.2.2 を使用して Http POST リクエストを実行しています。投稿リクエストに記載されている URL にアクセスするために .pfx 証明書を使用しています。しかし、302、一時的に移動エラーが発生しています

//Java コード

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();

    }
}
4

1 に答える 1

1

この行をコードに追加します。

client.setRedirectStrategy(new LaxRedirectStrategy());
于 2013-04-24T06:12:43.060 に答える