Java中的HTTP帖子不会在服务器端发布

时间:2014-05-28 14:54:51

标签: servlets post http-post httpresponse

我正在尝试使用HTTP post发布数据,但我无法在另一端(服务器)看到数据。 当我运行程序时,我得到一个响应代码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());

}

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

1 个答案:

答案 0 :(得分:1)

我已使用此网址“https://posttestserver.com/post.php?dir=example”测试了您的代码,似乎没问题。你确定问题不在服务器端吗?

   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