java 发送http和https请求

HTTP请求:
如果需要Json格式的自己转下,度娘上N种姿势…

//处理http请求  requestUrl为请求地址  requestMethod请求方式,值为"GET""POST"  
    public static String httpRequest(String requestUrl,String requestMethod,String outputStr){  
        StringBuffer buffer=null;  
        try{  
        URL url=new URL(requestUrl);  
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
        conn.setDoOutput(true);  
        conn.setDoInput(true);  
        conn.setRequestMethod(requestMethod);  
        conn.connect();  
        //往服务器端写内容 也就是发起http请求需要带的参数  
        if(null!=outputStr){  
            OutputStream os=conn.getOutputStream();  
            os.write(outputStr.getBytes("utf-8"));  
            os.close();  
        }  

        //读取服务器端返回的内容  
        InputStream is=conn.getInputStream();  
        InputStreamReader isr=new InputStreamReader(is,"utf-8");  
        BufferedReader br=new BufferedReader(isr);  
        buffer=new StringBuffer();  
        String line=null;  
        while((line=br.readLine())!=null){  
            buffer.append(line);  
        }  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
        return buffer.toString();  
    }  

HTTPS请求:
1、https和http的区别我这里就不介绍了,在java
中访问https链接的话需要有相应的SSL证书,如果没有就无法访问(使用http访问https会返回403),所以我们要先自定义一个信任管理器。实现java自带的X509TrustManger接口,代码:

import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;  
import javax.net.ssl.X509TrustManager;  

public class MyX509TrustManager implements X509TrustManager {  
    @Override  
    public void checkClientTrusted(X509Certificate[] chain, String authType)  
            throws CertificateException {  
        // TODO Auto-generated method stub  
    }  
    @Override  
    public void checkServerTrusted(X509Certificate[] chain, String authType)  
            throws CertificateException {  
        // TODO Auto-generated method stub  
    }  
    @Override  
    public X509Certificate[] getAcceptedIssuers() {  
        // TODO Auto-generated method stub  
        return null;  
    } 
}  

2、然后我们就可以使用https请求了:

/* 
 * 处理https GET/POST请求 
 * 请求地址、请求方法、参数 
 * */  
public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){  
    StringBuffer buffer=null;  
    try{  
    //创建SSLContext  
    SSLContext sslContext=SSLContext.getInstance("SSL");  
    TrustManager[] tm={new MyX509TrustManager()};  
    //初始化  
    sslContext.init(null, tm, new java.security.SecureRandom());;  
    //获取SSLSocketFactory对象  
    SSLSocketFactory ssf=sslContext.getSocketFactory();  
    URL url=new URL(requestUrl);  
    HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();  
    conn.setDoOutput(true);  
    conn.setDoInput(true);  
    conn.setUseCaches(false);  
    conn.setRequestMethod(requestMethod);  
    //设置当前实例使用的SSLSoctetFactory  
    conn.setSSLSocketFactory(ssf);  
    conn.connect();  
    //往服务器端写内容  
    if(null!=outputStr){  
        OutputStream os=conn.getOutputStream();  
        os.write(outputStr.getBytes("utf-8"));  
        os.close();  
    }  

    //读取服务器端返回的内容  
    InputStream is=conn.getInputStream();  
    InputStreamReader isr=new InputStreamReader(is,"utf-8");  
    BufferedReader br=new BufferedReader(isr);  
    buffer=new StringBuffer();  
    String line=null;  
    while((line=br.readLine())!=null){  
        buffer.append(line);  
    }  
    }catch(Exception e){  
        e.printStackTrace();  
    }  
    return buffer.toString();  
}  

参考自 http://blog.csdn.net/guozili1/article/details/53995121

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值