小编mar*_*ari的帖子

如何使用x-www-form-urlencoded正文发送帖子请求

邮递员要求

如何在java中,我可以发送请求x-www-form-urlencoded header.我不明白如何发送带有键值的正文,就像上面的截图一样.

我试过这段代码:

String urlParameters =
  cafedra_name+ data_to_send;
URL url;
    HttpURLConnection connection = null;  
    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");

      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  

      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();
Run Code Online (Sandbox Code Playgroud)

但在回复中,我没有收到正确的数据.

java

37
推荐指数
2
解决办法
9万
查看次数

标签 统计

java ×1