码迷,mamicode.com
首页 > 编程语言 > 详细

java http get和post请求

时间:2015-04-01 21:35:16      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

1.http工具类

package com.funshion.common.utils;

import java.net.URI;
import java.net.URL;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http工具类
 *
 * @author qiuwy
 *
 */
@SuppressWarnings({ "deprecation", "resource" })
public class HttpUtil {

    public static final int HTTP_CODE_200 = 200;

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * get
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static HttpResVo doGet(String getUrl) throws Exception {
        URL url = new URL(getUrl);
        URI uri = new URI(url.getProtocol(), null, url.getHost(), url.getPort(), url.getPath(), url.getQuery(), null);
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(uri);
        HttpResponse response = httpclient.execute(httpGet);

        int httpCode = response.getStatusLine().getStatusCode();
        String result = EntityUtils.toString(response.getEntity());

        logger.info("Get.url=" + uri + ",response code=" + httpCode + ",response entity=" + result);

        return new HttpResVo(httpCode, result);
    }

    /**
     * post
     *
     * @param url
     * @param params
     * @return 返回http code
     *
     */
    public static HttpResVo doPost(String url, String params) throws Exception {
        URL postUrl = new URL(url);
        URI uri = new URI(postUrl.getProtocol(), null, postUrl.getHost(), postUrl.getPort(), postUrl.getPath(), postUrl.getQuery(), null);
        
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(uri);
        
        StringEntity entity = new StringEntity(params, HTTP.UTF_8);
        entity.setContentType("application/json;charset=UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(entity);

        HttpResponse response = httpclient.execute(httpPost);
        int httpCode = response.getStatusLine().getStatusCode();
        String result = EntityUtils.toString(response.getEntity());
        
        logger.info("Post.url=" + uri + ",response code=" + httpCode + ",response entity=" + result);
        
        return new HttpResVo(httpCode, result);

    }

    /**
     * 根据参数和url访问远程,得到返回值
     *
     * @return
     */
    protected HttpResVo getHttpResult() throws Exception {
        if (StringUtils.isEmpty(this.method)) {
            logger.error("method is null.");
            return null;
        }

        // get方法的处理
        if (this.method.equalsIgnoreCase(GET)) {
            StringBuffer _url = new StringBuffer(this.url);
            if (!StringUtils.isEmpty(this.parameter)) {
                _url.append("?").append(this.parameter);
                System.out.println(_url.toString());
            }
            return HttpUtil.doGet(_url.toString());
        } else {// post方法处理
            if (!StringUtils.isEmpty(this.parameter)) {

                return HttpUtil.doPost(this.url, this.parameter);
            }
            logger.error("post method has parameter none.");
        }
        return null;
    }

}

2.http响应返回值

package com.funshion.common.utils;

/**
 * http响应返回值
 *
 * @author qiuwy
 *
 */
public class HttpResVo implements java.io.Serializable {

    public static final int STATUS_OK = 200;

    private static final long serialVersionUID = -3063948675189138529L;
    private int httpCode;
    private String result;

    public HttpResVo() {

    }

    public HttpResVo(int httpCode, String result) {
        this.httpCode = httpCode;
        this.result = result;
    }

    public int getHttpCode() {
        return httpCode;
    }

    public void setHttpCode(int httpCode) {
        this.httpCode = httpCode;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

}

 

java http get和post请求

标签:

原文地址:http://www.cnblogs.com/weiweiyao/p/4385283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!