Java https请求

来源:互联网 发布:永琪和知画圆房了几次 编辑:程序博客网 时间:2024/05/29 21:16
java https请求,需要依赖包:以下代码可复制,直接使用package com.up72.sjf.utils.HttpsClientSendPost;import java.io.*;import java.net.HttpURLConnection;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.net.UnknownHostException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.SocketFactory;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSession;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.ClientConnectionManager;import org.apache.http.conn.ConnectTimeoutException;import org.apache.http.conn.scheme.HostNameResolver;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.HttpConnectionParams;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;/* *  *  */public class HttpsClientSendPost {    /*public static void main(String[] args) {        String urlString = "https://open.ccchong.com/api/refreshToken";        String output = new String(HttpClientSendPost.sendXMLDataByGet(urlString,"appkey=8AArM9016w3H26B5j1&appsecret=wp7j1K4880eAMmjo4s3Rtkq44l949u3p"));        System.out.println(output);    }*/    private static DefaultHttpClient client;    /**     * 访问https的网站     * @param httpclient     */    private static void enableSSL(DefaultHttpClient httpclient) {        //调用ssl          try {            SSLContext sslcontext = SSLContext.getInstance("TLS");            sslcontext.init(null, new TrustManager[]{truseAllManager}, null);            SSLSocketFactory sf = new SSLSocketFactory(sslcontext);            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);            Scheme https = new Scheme("https", sf, 443);            httpclient.getConnectionManager().getSchemeRegistry().register(https);        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 重写验证方法,取消检测ssl     */    private static TrustManager truseAllManager = new X509TrustManager() {        public void checkClientTrusted(                java.security.cert.X509Certificate[] arg0, String arg1)                throws CertificateException {            // TODO Auto-generated method stub          }        public void checkServerTrusted(                java.security.cert.X509Certificate[] arg0, String arg1)                throws CertificateException {            // TODO Auto-generated method stub          }        public java.security.cert.X509Certificate[] getAcceptedIssuers() {            // TODO Auto-generated method stub              return null;        }    };    /**     * HTTP Client Object,used HttpClient Class before(version 3.x),but now the     * HttpClient is an interface     */    public static String sendXMLDataByGet(String url, String xml) {        // 创建HttpClient实例        if (client == null) {        // Create HttpClient Object            client = new DefaultHttpClient();            enableSSL(client);        }        StringBuilder urlString = new StringBuilder();        urlString.append(url);        urlString.append("?");        urlString.append(xml);        System.out.println("url------"+urlString);        String urlReq = urlString.toString();        // 创建Get方法实例             HttpGet httpsgets = new HttpGet(urlReq);        String strRep = "";        try {            HttpResponse response = client.execute(httpsgets);            HttpEntity entity = response.getEntity();            if (entity != null) {                strRep = EntityUtils.toString(response.getEntity());                // Do not need the rest                httpsgets.abort();            }        } catch (ClientProtocolException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IllegalStateException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return strRep;    }    /**     * Send a XML-Formed string to HTTP Server by post method     *     * @param url     the request URL string     * @param xmlData XML-Formed string ,will not check whether this string is     *                XML-Formed or not     * @return the HTTP response status code ,like 200 represents OK,404 not     * found     * @throws IOException     * @throws ClientProtocolException     */    public static String sendXMLDataByPost(String url, String xmlData)            throws ClientProtocolException, IOException {        if (client == null) {            // Create HttpClient Object            client = new DefaultHttpClient();            enableSSL(client);        }        client.getParams().setParameter("http.protocol.content-charset",                HTTP.UTF_8);        client.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);        client.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);        client.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET,                HTTP.UTF_8);        // System.out.println(HTTP.UTF_8);        // Send data by post method in HTTP protocol,use HttpPost instead of        // PostMethod which was occurred in former version        // System.out.println(url);        HttpPost post = new HttpPost(url);        post.getParams().setParameter("http.protocol.content-charset",                HTTP.UTF_8);        post.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);        post.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);        post.getParams()                .setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8);        // Construct a string entity        StringEntity entity = new StringEntity(getUTF8XMLString(xmlData), "UTF-8");        entity.setContentType("text/xml;charset=UTF-8");        entity.setContentEncoding("UTF-8");        // Set XML entity        post.setEntity(entity);        // Set content type of request header        post.setHeader("Content-Type", "text/xml;charset=UTF-8");        // Execute request and get the response        HttpResponse response = client.execute(post);        HttpEntity entityRep = response.getEntity();        String strrep = "";        if (entityRep != null) {            strrep = EntityUtils.toString(response.getEntity());            // Do not need the rest                post.abort();        }        // Response Header - StatusLine - status code        // statusCode = response.getStatusLine().getStatusCode();        return strrep;    }    /**     * Get XML String of utf-8     *     * @return XML-Formed string     */    public static String getUTF8XMLString(String xml) {        // A StringBuffer Object        StringBuffer sb = new StringBuffer();        sb.append(xml);        String xmString = "";        try {            xmString = new String(sb.toString().getBytes("UTF-8"));        } catch (UnsupportedEncodingException e) {        // TODO Auto-generated catch block            e.printStackTrace();        }        // return to String Formed        return xmString.toString();    }    public static String sendPost(String url,String param ) {        PrintWriter out = null;        BufferedReader in = null;        String result = "";        try {            URL realUrl = new URL(url);            // 打开和URL之间的连接            URLConnection conn = realUrl.openConnection();            // 设置通用的请求属性            conn.setRequestProperty("accept", "*/*");            conn.setRequestProperty("connection", "Keep-Alive");            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");            // 发送POST请求必须设置如下两行            conn.setDoOutput(true);            conn.setDoInput(true);            // 获取URLConnection对象对应的输出流//            out = new PrintWriter(conn.getOutputStream(),"utf-8");            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));            // 发送请求参数            out.print(param);            // flush输出流的缓冲            out.flush();            // 定义BufferedReader输入流来读取URL的响应            /*in = new BufferedReader(new InputStreamReader(conn.getInputStream()));*/            // 定义BufferedReader输入流来读取URL响应            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));            String line;            while ((line = in.readLine()) != null) {                result += line;            }            while(in.readLine()!=null){                line=in.readLine();                result +=line;            }        } catch (Exception e) {            System.out.println("发送 POST 请求出现异常!"+e);            e.printStackTrace();        }        //使用finally块来关闭输出流、输入流        finally{            try{                if(out!=null){                    out.close();                }                if(in!=null){                    in.close();                }            }            catch(IOException ex){                ex.printStackTrace();            }        }        return result;    }}
0 0