HttpClient如何解決302重定向問題

最近的接口測試,發現接口地址報302錯誤,經過上網搜索,發現問題所在,解決辦法是須要請求重定向後的URI。apache

package com.btv;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/** 
 * @author QiaoJiafei 
 * @version 建立時間:2016年1月5日 上午10:17:46 
 * 類說明 
 */
public class TestLogin {
    public static void main(String args[]) {
        try {
            HttpClient client = HttpClients.createDefault();
            login(client);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void login( HttpClient client) throws Exception{
            final String APPLICATION_JSON = "application/json";
          final String CONTENT_TYPE_TEXT_JSON = "text/json";
          
          
        String url = "http://172.16.30.208:8092/svc/login";
            String js = "{\"username\":\"13800000002\",\"password\":\"123456\"}";
          
        HttpPost httpPost = new HttpPost(url);       
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

        
        StringEntity se = new StringEntity(js);
        se.setContentType(CONTENT_TYPE_TEXT_JSON);

        httpPost.setEntity(se);
        
        HttpResponse response = null;
        
        response = client.execute(httpPost);

        //----------判斷是否重定向開始
        int code = response.getStatusLine().getStatusCode();
        String newuri="";
        if (code == 302) {
            Header header = response.getFirstHeader("location"); // 跳轉的目標地址是在 HTTP-HEAD 中的
            newuri = header.getValue(); // 這就是跳轉後的地址,再向這個地址發出新申請,以便獲得跳轉後的信息是啥。
            System.out.println(newuri);
            System.out.println(code);
            
            httpPost = new HttpPost(newuri);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
             
            se = new StringEntity(js);
            se.setContentType(CONTENT_TYPE_TEXT_JSON);

            httpPost.setEntity(se);

            response = client.execute(httpPost);
            code = response.getStatusLine().getStatusCode();
            System.out.println("login"+code);
        }

        //------------重定向結束
        HttpEntity entity = null;
        entity = response.getEntity();
        String s2 = EntityUtils.toString(entity, "UTF-8");
        System.out.println(s2);
        
        
    }
    
    
    
}
相關文章
相關標籤/搜索