【问题标题】:How to send requests over ssl?如何通过 ssl 发送请求?
【发布时间】:2013-11-23 09:28:46
【问题描述】:

我正在尝试从 localhost 向服务器发送请求,但它返回以下错误。

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;


public class Test {

 public void connectMyServer(){

        Login auth = new Login("username", "password");

        JAXBContext cntx = JAXBContext.newInstance(Login.class);
        Marshaller m = cntx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        try {
            URL url = new URL("https://www.server.com/requests");
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setDoOutput(true);
            con.setDoInput(true);

            OutputStream os = con.getOutputStream();
            m.marshal(auth, os);
            m.marshal(auth, System.out);

            os.flush();
            con.getResponseCode();

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
            con.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

  • 试试 HttpsURLConnection 而不是 HttpURLConnection
  • @smeaggie 我将其更改为 https 但仍然遇到相同的错误
  • 我同意 Jim 的看法;然后服务器不发送 HTTPS。
  • @smeaggie 当我将地址更改为 http 并发送 http 请求时,它显示错误 403
  • @smeaggie 请看看我的另一个问题stackoverflow.com/questions/20190364/…

标签: java ssl jaxb httpclient httpurlconnection


【解决方案1】:

当您应该使用 HttpsURLConnection 时,您正在尝试将 HttpURLConnection 用于 SSL。

编辑:

我试过了:

import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

public class Test
{

    public static void main(String[] args) throws Exception
    {
        URL url = new URL("https://www.google.com");
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        Map<String,List<String>> fields = con.getHeaderFields();
        con.disconnect();
    }
}

它运行没有问题。

无论您的技术团队说什么,您的错误消息都表明远程服务器正在响应,就好像它使用 HTTP 而不是 HTTPS。

【讨论】:

  • 远程服务器是否重定向到 HTTP?您是否使用 Wireshark 查看流量以了解发送和接收的内容?
  • 服务器技术团队建议我将所有连接发送到 https,所以我想他们不会将其重定向到 http
  • 我把它改成了https,但它显示同样的错误,这是否意味着他们不会说https?
  • 当我将地址更改为 http 并发送 http 请求时,它显示错误 403
  • 403 被禁止,这意味着您没有提供正确的身份验证。这意味着它绝对不是 HTTPS。您确实需要使用 Wireshark 捕获网络流量,以便向您的技术人员展示正在发生的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 2017-10-09
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
相关资源
最近更新 更多