带参数的https认证

Https authentification with parameters

我正在使用 HttpsURLConnection 连接到应用程序。一切都很好,直到我尝试在请求中使用参数(如用户名和密码),因为在服务器端空值被映射到参数。

     HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

         connection.addRequestProperty("password", "xxxx");
         connection.addRequestProperty("username", "aaaaaa");
         connection.addRequestProperty("eventId","btnLogin");

         connection.setRequestMethod("POST"); //$NON-NLS-1$
         connection.setDoOutput(true);
         connection.setDoInput(true);
         connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
         connection.setRequestProperty( "Accept", "*/*" );



         BufferedReader in = new BufferedReader(
                 new InputStreamReader(
                 connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}

我提到 HttpsURLConnection 有效,初始登录页面是通过 HttpsUrlConnection 检索的。

我如何通过 POST 请求在服务器端成功发送参数。

此致,

我为了解决这个问题,我创建了一个对 HashMap 对象的 Map 引用,我在该对象的键中存储了参数,在值中存储了这些参数的值。

之后,我创建了一个方法,该方法将 return 一个包含用于 POST 请求的编码参数的字符串对象。

private String getOutput(Map<String, String> hm) throws UnsupportedEncodingException {

       String out = "";
       boolean start = true;

       for(Map.Entry<String, String> h : hm.entrySet()){
           if(start){

               start=false;
               out+=URLEncoder.encode(h.getKey(),  "UTF-8");
               out+="=";
               out+=URLEncoder.encode(h.getValue(), "UTF-8");
           }


           else{
               out += "&";
               out+=URLEncoder.encode(h.getKey(),  "UTF-8");
               out+="=";
               out+=URLEncoder.encode(h.getValue(), "UTF-8");
           }

       }

以上所有内容都在输出流中使用,以便将它们发送到服务器。

  out = new BufferedWriter(
                  new OutputStreamWriter(connection.getOutputStream())
                  );
          out.write(getOutput(hm));

          out.close();