关于HttpClient请求https网页,需要做什么处理么?还是就和处理http一样直接照通常方法请求?

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;public class UrlConnect { public static void main(String[] args) {
    new NetWin();
    }}class NetWin extends JFrame implements ActionListener, Runnable {

    JButton button;
    URL url;
    JTextField text;
    JEditorPane area;
    byte b[] = new byte[118];
    Thread thread;
    String URL = "http://www.baidu.com/s?wd=";
    String fin; private Color white; NetWin() {
    text = new JTextField(20);
    area = new JEditorPane();
    button = new JButton("查询");
    button.addActionListener(this);
    thread = new Thread(this);
    JPanel p = new JPanel();
    p.add(new JLabel("连接百度,请输入关键字:"));
    p.add(text);
    p.add(button);
    p.setBackground(white);
    add(area, BorderLayout.CENTER);
    add(p, BorderLayout.NORTH);
    setBounds(60, 60, 600, 400);
    setVisible(true);
    validate();
    } public void actionPerformed(ActionEvent e) {
    if (!(thread.isAlive()))
    thread = new Thread(this);
    try {
    thread.start();
    } catch (Exception ee) {
    text.setText("我正在读取" + url);
    }
    } public void run() {
    try {
    int n = -1;
    area.setText(null);
    area.setVisible(true);
    fin = text.getText();
    URL += fin;
    url = new URL(URL);
    InputStream in = url.openStream();
    while ((n = in.read(b)) != -1) {
    String s = new String(b, 0, n);
    area.setPage(URL);
    }
    } catch (MalformedURLException e1) {
    text.setText("" + e1);
    return;
    } catch (IOException e1) {
    text.setText("" + e1);
    return;
    }
    }}
      

  2.   

    应该是一样的,https是通过证书加密的,这是服务器端的事,和你请求应该没有什么关系。
    请求URL直接从http://变成https应该可以。具体我倒是没有试验过。
      

  3.   

    请参考 http://yjhexy.javaeye.com/blog/576964 这篇文章,我以前按照这篇文章已经成功实现了使用httpclient访问需要pfx文件的https网站。
    不过里面有一句话是错的,它里面写:“首先把a.pfx导入到IE。然后用IE的Internet选项--》内容--》证书--》导出成不带私钥的cer格式就搞定了”,其实这样导出的cer文件是错的,应该这么做:通过浏览器访问时第二下弹出的“安全警报”—>“查看证书”—>“详细信息”—>“复制到文件”……导出cer格式的文件,这个cer文件才是正确的。