Servlets Java中的HTTP post不在服务器端进行post

Servlets Java中的HTTP post不在服务器端进行post,servlets,post,http-post,httpresponse,Servlets,Post,Http Post,Httpresponse,我试图使用HTTPPOST发布数据,但在另一端(服务器)上看不到数据。 当我运行程序时,我得到一个响应代码200(成功),但当我交叉检查时,在另一端没有找到任何数据 private void sendPost() throws Exception { String url = "https://example.com/post/index.php"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLC

我试图使用HTTPPOST发布数据,但在另一端(服务器)上看不到数据。 当我运行程序时,我得到一个响应代码
200
(成功),但当我交叉检查时,在另一端没有找到任何数据

private void sendPost() throws Exception {

    String url = "https://example.com/post/index.php";
    URL obj = new URL(url);

    HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();


    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("charset", "utf-8");



    String urlParameters = "username=userpv&password=pvpwd&admin_username=&admin_password=&action=add&requisition_number=403555&return_url=&error_url=www.some.com&error_format=1";

    // urlParameters string is very long

    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));


    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
    }
    in.close();

    //print result
    //System.out.println(response.toString());

}

有人能告诉我我的代码有什么问题吗?

我已经用这个url“”测试了你的代码,看起来还可以。您确定问题不在服务器端吗

   public static void main (String[] args) throws IOException {

      String url = "https://posttestserver.com/post.php?dir=example";
      URL obj = new URL(url);

      HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();

      //add reuqest header
      con.setRequestMethod("POST");
      con.setRequestProperty("User-Agent", "AGENT");
      con.setRequestProperty("charset", "utf-8");     

      String urlParameters = "username=userpv&password=pvpwd&admin_username=&admin_password=&action=add&requisition_number=403555&return_url=&error_url=www.some.com&error_format=1";

      // urlParameters string is very long

      con.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(con.getOutputStream());
      wr.writeBytes(urlParameters);
      wr.flush();
      wr.close();

      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + urlParameters);
      System.out.println("Response Code : " + responseCode);

      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
          System.out.println(inputLine);
      }
      in.close();

      //print result
      //System.out.println(response.toString());      
   }
这是posttestserver的结果:

Time: Mon, 25 Aug 14 00:51:01 -0700
Source ip: xxx.xxx.xxx.xxx

Headers (Some may be inserted by server)
REQUEST_URI = /post.php?dir=example
QUERY_STRING = dir=example
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 54235
REMOTE_ADDR = xxx.xxx.xxx.xxx
CONTENT_LENGTH = 149
CONTENT_TYPE = application/x-www-form-urlencoded
HTTP_CONNECTION = close
HTTP_ACCEPT = text/html, image/gif, image/jpeg, *; q=.2
HTTP_HOST = posttestserver.com
HTTP_CHARSET = utf-8
HTTP_USER_AGENT = AGENT
HTTPS = on
UNIQUE_ID = U-rq5dBx6hIAAHdXN9wAAAAL
REQUEST_TIME_FLOAT = 1408953061.0627
REQUEST_TIME = 1408953061

Post Params:
key: 'username' value: 'userpv'
key: 'password' value: 'pvpwd'
key: 'admin_username' value: ''
key: 'admin_password' value: ''
key: 'action' value: 'add'
key: 'requisition_number' value: '403555'
key: 'return_url' value: ''
key: 'error_url' value: 'www.some.com'
key: 'error_format' value: '1'
Empty post body.

Upload contains PUT data:
username=userpv&password=pvpwd&admin_username=&admin_password=&action=add&requisition_number=403555&return_url=&error_url=www.some.com&error_format=1

有人能帮我吗?有同样的问题……我也尝试过Apache HttpCliente,但仍然是一样的。我用
www
前缀调用
URL
,并通过删除它来解决。您实际上是在不使用它的情况下调用它,但使用的是
https
。使用
http
进行一些测试,看看它是否有效。此外,我注意到您正在设置用户代理,而不是内容类型。尝试删除useragent并添加内容类型:
application/x-www-form-urlencoded;字符集=UTF-8