如何通过SSL发送请求?

5
我正在尝试从本地主机向服务器发送请求,但返回以下错误。
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
@smeaggie 我已经将它改为 https 但仍然遇到相同的错误。 - user1386522
我和吉姆的看法一致,服务器没有发送HTTPS。 - smeaggie
@smeaggie 当我将地址改为http并发送http请求时,它显示错误403。 - user1386522
@smeaggie 请看一下我的另一个问题 http://stackoverflow.com/questions/20190364/how-to-bypass-certificateexception-by-java - user1386522
1个回答

1
您正在尝试使用HttpURLConnection进行SSL,而应该使用HttpsURLConnection
编辑:
我尝试了这个:
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查看流量,看看发送和接收的内容是什么? - Jim Garrison
服务器技术团队建议我将所有连接发送到https,因此我认为他们不会将其重定向到http。 - user1386522
我把它改成了https,但是仍然显示相同的错误,这是否意味着他们不支持https? - user1386522
当我将地址更改为http并发送http请求时,它显示403错误。 - user1386522
403是禁止访问的,这意味着您没有提供正确的身份验证。这意味着它绝对不是HTTPS。您真的需要使用Wireshark捕获网络流量,以便向技术人员展示发生了什么。 - Jim Garrison
请查看我的另一个问题:http://stackoverflow.com/questions/20190364/how-to-bypass-certificateexception-by-java - user1386522

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接