我使用“Https”协议创建了与服务器的连接。这是我的代码......
String httpsURL = "https://abc.com/auth/login/";
HttpsURLConnection con = null;
try{
URL url = new URL(httpsURL);
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST" );
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
//String respMessage = con.getResponseMessage();
//int respCode = con.getResponseCode();
}catch(....){....}
现在我必须通过该连接将我的JSON对象发送到服务器。我怎样才能做到这一点?请帮我。提前谢谢。
答案 0 :(得分:1)
也许这可能会有所帮助......我不知道您的代码的具体用途。
String httpsURL = "https://abc.com/auth/login/";
HttpsURLConnection con = null;
try{
URL url = new URL(httpsURL);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
//con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Length", Integer.toString(jsonValue.length()));
con.getOutputStream().write(jsonValue.getBytes());
con.getOutputStream().flush();
con.connect();
if (con.getResponseCode() != HttpsURLConnection.HTTP_OK) {
throw new Exception("POST method failed: " + con.getResponseCode() + "\t" + con.getResponseMessage());
} else {
InputStream responseContent = (InputStream) con.getContent();
}
...
}catch(....){....}