使用HttpsURLConnection做https连接,注意返回是否的字符encoding,有可能是gzip格式

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.zip.GZIPInputStream;

/**
 * Created by dell on 2018/4/1.
 */
public class HttpsTest {

    private static Header[] headers = {new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +
            "Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)"),
            new BasicHeader("Accept-Language", "zh-cn"),
            new BasicHeader("Accept", " image/gif, image/x-xbitmap, image/jpeg, " +
                    "image/pjpeg, application/x-silverlight, application/vnd.ms-excel, " +
                    "application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"),
            new BasicHeader("Content-Type", "application/x-www-form-urlencoded"),
            new BasicHeader("Accept-Encoding", "gzip, deflate")};

    public static String get(String url, Map<String, String> params) throws IOException {
        CloseableHttpClient httpClient = null;
        String res = null;
        try {
            httpClient = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            get.setHeaders(headers);
            //如果有参数则设置参数
            //StringEntity是最简单的传输类型,UrlEncodedFormEntity继承StringEntity,一般用于表单上传,在StringEntity中,表单项的格式是key=value&key=value,
            // 因为这里只有一项表单,所以我用StringEntity
//            StringEntity stringEntity = new StringEntity("param="+JSON.toJSONString(params), ContentType.create("application/x-www-form-urlencoded", "UTF-8"));
//            //这个设置同上面的设置是不一样的,上面的UTF-8是解析StringEntity中的传入string的字符集,将其转换为byte
//            stringEntity.setContentEncoding("utf-8");
//            post.setEntity(stringEntity);

//
//            List<BasicNameValuePair> paramList = new ArrayList<BasicNameValuePair>();
//            paramList.add(new BasicNameValuePair("param", JSON.toJSONString(params)));
//            post.setEntity(new UrlEncodedFormEntity(paramList, "UTF-8"));


            HttpResponse response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                System.out.println("错误码[" + statusCode + "]");
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                res = EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (null != httpClient) {
                httpClient.close();
            }
        }
        return res;
    }


    public static void main(String[] args) throws IOException {
        URL reqURL = new URL("https://www.baidu.com/" ); //创建URL对象
        HttpsURLConnection httpsConn = (HttpsURLConnection)reqURL.openConnection();
//        httpsConn.setRequestProperty("Host", "leetcode.com");
        httpsConn.setRequestProperty("contentType", "utf-8");
        httpsConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");
        httpsConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpsConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        httpsConn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        httpsConn.setRequestProperty("Connection", "keep-alive");
        httpsConn.setRequestProperty("Upgrade-Insecure-Requests", "1");
        httpsConn.setRequestProperty("Cache-Control", "max-age=0");
        String encoding = httpsConn.getContentEncoding();

        BufferedReader buffread = null;
        if (encoding != null && encoding.contains("gzip")) {//首先判断服务器返回的数据是否支持gzip压缩,
            //如果支持则应该使用GZIPInputStream解压,否则会出现乱码无效数据
            buffread = new BufferedReader(new InputStreamReader(new GZIPInputStream(httpsConn.getInputStream()),"utf-8"));
        }else {

            //取得该连接的输入流,以读取响应内容
            buffread = new BufferedReader(new InputStreamReader(httpsConn.getInputStream(), "utf-8"));
        }
        //编码问题,这边处理一下就ok了,不错 啊啊啊,对流进行转码
        String line;
        line=buffread.readLine();
        while(line!=null){
            line=buffread.readLine();
            System.out.println(line);
        }

//        String res = get("https://bladeandsoul.gamepedia.com/Mushin",null);
//        System.out.println(res);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值