java通过Http get post请求url获取数据

package com.avic.core.utils;

import java.io.BufferedReader;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.net.HttpURLConnection;  
import java.net.URL;  
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;  
public class HttpGetRequest {  
  
    /** 
     * Get Request 
     *  
     * @return 
     * @throws Exception 
     */  
    public static String doGet(String url) throws Exception {  
        URL localURL = new URL(url);  
        URLConnection connection = localURL.openConnection();  
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;  
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");  
        httpURLConnection.setRequestProperty("Content-Type",  
                "application/text");  
  
        InputStream inputStream = null;  
        InputStreamReader inputStreamReader = null;  
        BufferedReader reader = null;  
        StringBuffer resultBuffer = new StringBuffer();  
        String tempLine = null;  
  
        if (httpURLConnection.getResponseCode() >= 300) {  
            throw new Exception(  
                    "HTTP Request is not success, Response code is "  
                            + httpURLConnection.getResponseCode());  
        }  
  
        try {  
            inputStream = httpURLConnection.getInputStream();  
            inputStreamReader = new InputStreamReader(inputStream);  
            reader = new BufferedReader(inputStreamReader);  
  
            while ((tempLine = reader.readLine()) != null) {  
                resultBuffer.append(tempLine);  
            }  
  
        } finally {  
  
            if (reader != null) {  
                reader.close();  
            }  
  
            if (inputStreamReader != null) {  
                inputStreamReader.close();  
            }  
  
            if (inputStream != null) {  
                inputStream.close();  
            }  
  
        }  
        return resultBuffer.toString();  
    }  
    /**
     * Http post 
     * @param url post请求url
     * @param params 参数
     * @return
     * @throws Exception
     */
    public static String doPost(String url,Map<String, String> params) throws Exception {  
    	   HttpPost httpPost=new HttpPost(url);
		   HttpClient httpclient=new DefaultHttpClient();
		   List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
		   if(params!=null){
			   BasicNameValuePair bnvp=null;
			   for (Map.Entry<String, String> p : params.entrySet()) {
				   bnvp=new BasicNameValuePair(p.getKey(), p.getValue());
			 }
		   }
	       httpPost.setEntity(new UrlEncodedFormEntity(nvps));  
	       HttpResponse response = httpclient.execute(httpPost);
	       HttpEntity respEntity = response.getEntity();//获得返回数据  
	       String text = EntityUtils.toString(respEntity, "UTF-8");  
	       JSONObject obj = (JSONObject) JSONObject.parse(text);  
	       httpclient.getConnectionManager().shutdown();  
    	return obj.toJSONString();
    }
}  

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值