0
我正在使用http url,但现在我需要更改为https。 这是我的代码:如何在Android中执行HTTPS POST请求?
throws IOException {
String result = "";
try {
String strPost = "";
for(String key : post.keySet()){
strPost+= key+"="+post.get(key)+"&";
}
strPost = strPost.substring(0,strPost.length()-1);
URL urlPost = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection)urlPost.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
urlConnection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
urlConnection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(urlConnection.getOutputStream());
dStream.writeBytes(strPost);
dStream.flush();
dStream.close();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line = "";
StringBuilder responseOutput = new StringBuilder();
while((line = br.readLine()) != null) {
responseOutput.append(line);
}
br.close();
result = responseOutput.toString();
}catch (Exception ex){
}
return result;
}
我试图使用HttpsURLConnection的,但它不工作。我看了一些关于这个问题的其他问题,但它也没有工作。