http Client 요청 및 데이터 반환 처리

7066 단어 JAVA

import java.io.IOException;
import java.net.URLDecoder;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jspxcms.core.domain.Auth;

public class HttpRequestUtils {
    private static Logger logger = LoggerFactory.getLogger(HttpRequestUtils.class);    //    

   /**
     * httpPost
     * @param url    
     * @param jsonParam   
     * @return
     */
    public static JSONObject httpPost(String url,JSONObject jsonParam){
        return httpPost(url, jsonParam, false);
    }

    /**
     * post  
     * @param url         url  
     * @param jsonParam       
     * @param noNeedResponse           
     * @return
     */
    public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){
        //post      
        DefaultHttpClient httpClient = new DefaultHttpClient();
        JSONObject jsonResult = null;
        HttpPost method = new HttpPost(url);
        try {
            if (null != jsonParam) {
                //        
                StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            HttpResponse result = httpClient.execute(method);
            url = URLDecoder.decode(url, "UTF-8");
            /**      ,     **/
            if (result.getStatusLine().getStatusCode() == 200) {
                String str = "";
                try {
                    /**          json     **/
                    str = EntityUtils.toString(result.getEntity());
                    if (noNeedResponse) {
                        return null;
                    }
                    /** json      json  **/
                    jsonResult = JSONObject.fromObject(str);
                } catch (Exception e) {
                    logger.error("post      :" + url, e);
                }
            }
        } catch (IOException e) {
            logger.error("post      :" + url, e);
        }
        return jsonResult;
    }


    /**
     *   get  
     * @param url      
     * @return
     */
    public static JSONObject httpGet(String url){
        //get      
        JSONObject jsonResult = null;
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            //  get  
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);

            /**      ,     **/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**          json     **/
                String strResult = EntityUtils.toString(response.getEntity());
                /** json      json  **/
                jsonResult = JSONObject.fromObject(strResult);
                url = URLDecoder.decode(url, "UTF-8");
            } else {
                logger.error("get      :" + url);
            }
        } catch (IOException e) {
            logger.error("get      :" + url, e);
        }
        return jsonResult;
    }
    public static void main(String[] args) {
        String username = "admin";
        String password = "123";
        Auth user = new Auth();
        user.setClientId(username);
        user.setClientSecret(password);
        JSONObject json =JSONObject.fromObject(user);

    }
}

좋은 웹페이지 즐겨찾기