【发布时间】: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