将发布请求发送到https

时间:2013-07-11 08:18:23

标签: java post https

我需要将帖子请求发送到https地址。我有一个功能,可以发送邮件,但我似乎无法使其适用于https。

public static String serverCall(String link, String data){
     HttpURLConnection connection;
     OutputStreamWriter request = null;

         URL url = null;
         String response = null;
         String parameters = data;

         try
         {
             url = new URL(link);
             connection = (HttpURLConnection) url.openConnection();
             connection.setDoOutput(true);
             connection.setRequestProperty("Content-Type", "text/xml");
             connection.setRequestMethod("POST"); 

             request = new OutputStreamWriter(connection.getOutputStream());
             request.write(parameters);
             request.flush();
             request.close();
             String line = "";
             InputStreamReader isr = new InputStreamReader(connection.getInputStream());
             BufferedReader reader = new BufferedReader(isr);
             StringBuilder sb = new StringBuilder();
             while ((line = reader.readLine()) != null)
             {
                 sb.append(line + "\n");
             }
             // Response from server after  process will be stored in response variable.
             response = sb.toString();

             isr.close();
             reader.close();

         }
         catch(IOException e)
         {
             // Error
         }

         return response;

}

我尝试使用HttpURLConnection的HttpsURLConnection,我仍然从我的服务器获取null。

1 个答案:

答案 0 :(得分:0)

你应该致电connect();

....
connection.setRequestMethod("POST");
connection.connect();
....