Json 错误:HTTP/1.1 302已临时移动
我正在使用HTTPClient 4.2.2执行Http POST请求。我正在使用.pfx证书访问post请求中提到的URL。但我得到302,暂时移动错误 //Java代码Json 错误:HTTP/1.1 302已临时移动,json,keystore,apache-httpclient-4.x,Json,Keystore,Apache Httpclient 4.x,我正在使用HTTPClient 4.2.2执行Http POST请求。我正在使用.pfx证书访问post请求中提到的URL。但我得到302,暂时移动错误 //Java代码 public class CertificateAuth { private static final long TIMEOUT = 500000000L; //set trust store to be used to trust server certificate private Strin
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();
}
}
public-class-CertificateAuth{
专用静态最终长超时=500000000L;
//设置用于信任服务器证书的信任存储
私有字符串tokapipostrl=”http://test.com/l1/rest1/lt/v1/data";
私有字符串tokenPost=“{\“id\”:\“Token\u 15555\”,\“type\”:“Token\”,\“entity\u type\”:“Store\”,\“entity\u id\”:“StoreId\”,\“expiration\u time\”:1376579410};
//客户端被视为可变类,以便服务器设置的cookie在
//多次呼叫
私有HttpClient=null;
公共证书(){
}
公共字符串createToken()引发异常{
//设置合理的超时时间,因为我们似乎永远都在等待响应:
KeyStore KeyStore=KeyStore.getInstance(“pkcs12”);
InputStream keystoreInput=新文件InputStream(“abc.pfx”);
load(keystoreInput,“password.toCharArray());
SchemeRegistry SchemeRegistry=新SchemeRegistry();
SSLSocketFactory lSchemeSocketFactory=新的SSLSocketFactory(密钥库,“qwerty10”);
register(新方案(“http”,PlainSocketFactory.getSocketFactory(),80));
register(新方案(“https”,443,lSchemeSocketFactory));
最终HttpParams HttpParams=新的基本HttpParams();
client=新的DefaultHttpClient(新的SingleClientConnManager(httpParams,schemeRegistry),httpParams);
字符串版本=null;
HttpPost HttpPost=新的HttpPost(Tokapipostrl);
//httpPost.getParams().setParameter(ClientPNames.HANDLE_重定向,Boolean.TRUE);
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,true);
试一试{
Map headersParameters=新建HashMap();
JSONObject jsonObj=新的JSONObject(tokenPost);
setParametersJson(httpPost、headersParameters、jsonObj);
HttpResponse resp=client.execute(httpPost);
如果(resp.getStatusLine().getStatusCode()==HttpURLConnection.HTTP|u确定| resp.getStatusLine().getStatusCode()==HttpURLConnection.HTTP|u已创建){
System.out.println(“成功查询”);
}
}最后{
httpPost.releaseConnection();
}
返回版本;
}
私有void setParametersJson(HttpRequestBase httpOperation、映射头参数、JSONObject JSONObject){
对于(字符串headerName:headerParameters.keySet()){
httpOperation.setHeader(headerName,headerParameters.get(headerName));
}
if(jsonObject!=null){
试一试{
StringEntity StringEntity=新的StringEntity(jsonObject.toString());
if(httpOperation instanceof HttpPost){
((HttpPost)httpOperation).setEntity(stringEntity);
}else if(httpOperation instanceof HttpPut){
((HttpPut)httpOperation).setEntity(stringEntity);
}
}捕获(不支持DencodingException ex){
例如printStackTrace();
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
公共静态void main(字符串[]args)引发异常{
CertificateAuth ua=新的CertificateAuth();
ua.createToken();
}
}
将此行添加到代码中
client.setRedirectStrategy(new LaxRedirectStrategy());
我添加了line client.setRedirectStrategy(新的laxrirectStrategy());但是它没有发布请求。上面的代码适用于HttpGet,但是从HttpPost和HttpPut获得302错误