【问题标题】:Sending post request to website向网站发送 post 请求
【发布时间】:2015-09-20 06:25:43
【问题描述】:

我正在尝试使用 java 登录到网站,然后最终检索 cookie 以访问该网站上的信息。似乎我的帖子请求正在运行,但我收到了 500 的响应代码。我想知道这是否是因为我错误地格式化了帖子数据

当使用网站时,它说帖子的格式如下

{userName: "dfh", password: "suyj"}    

在我的代码中我使用了这个

String urlParameters = "userName:dfh,Password:suyj";

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

我是否认为这可能是问题所在,如果是,我该如何更正我的帖子数据格式?

下面是我的全部代码

public  class post {
public static void main(String[] args) throws IOException {
final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";


    String url = "website";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setRequestProperty("Accept-Encoding", "gzip, deflate");
    con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
    con.setRequestProperty("Referer", "https://extbasicph05.podc.sl.edst.ibm.com/FFAMobileRelay/");
    con.setRequestProperty("Content-Length", "64");
    con.setRequestProperty("Connection", "keep-alive");
    con.setRequestProperty("Referer", "https://extbasicph05.podc.sl.edst.ibm.com/FFAMobileRelay/");
    con.setRequestProperty("Cookies", "UnicaNIODID=KGew34gcvZ5-Y2NoBQr; mmid=-1658088866%7CKAAAAAr15Tc8FgsAAA%3D%3D; mmcore.pd=1484641984%7CejEyAAoBQvXlNzwWCxPPXGheAVibftSXgNJIDwAAADoOVvoDstFIAAAAAP//////////AAZEaXJlY3QBFgwIAAYABQAAAAAAAP+MAACAigAA/4wAAAIAxDEAAABFBVUW6QsA/////wHpCxoM//83AAAAAAAAAAOSfwAAusgAAJN/AAAgyAAAlH8AACHIAAABIYAAAAIAAADVNgAAAPcCB+70CwD/////AfQLHQz//60CAAEAAAAAAX+KAAAp2gAAAoCKAABAWBAAgYoAALMAAAAAAAABRQ%3D%3D; mmcore.srv=ldnvwcgus03; IBM_W3SSO_ACCESS=w3-03.sso.ibm.com; CoreID6=32675392371714128784599&ci=51040000|IBMTEST_51040000|IBMTESTW3_50200000|IBMTESTWWW; CoreM_State=75~-1~-1~-1~-1~3~3~5~3~3~7~7~|~~|~~|~~|~||||||~|~~|~~|~~|~~|~~|~~|~~|~; CoreM_State_Content=6~|~EE9EDB09923CD77F~|~0; PrefID=222-11978519; CSList=23427744/17540903,0/0,0/0,0/0,0/0; _ga=GA1.2.233924805.1433836377; mmcore.tst=0.169; ibmSurvey=1435758135373; 50200000_clogin=v=1&l=1435758136&e=1435760174616");

    String urlParameters = "userName:dfh,Password:suyj";

    // Send post request
    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);
    System.out.println(con.getErrorStream());

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

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

}

}

【问题讨论】:

  • 通常表单参数被 url 编码到 POST 正文中,例如“name1=value1&name2=value2”。您可以使用 Chrome 开发者工具/firebug/etc 下的网络选项卡来查看正在发送的确切内容。
  • 我用那个点击找到了 {userName: "dfh", password: "suyj"} 我应该在看别的东西吗

标签: java html http post web


【解决方案1】:

你已经发现了你的问题。修一下就好了... 而不是String urlParameters = "userName:dfh,Password:suyj";,使用

String urlParameters = "{userName:\"dfh\",Password:\"suyj\"}";

虽然我相信您的服务器需要 JSON 格式的 post 参数。在这种情况下,您应该使用,

String urlParameters = "{\"userName\":\"dfh\",\"Password\":\"suyj\"}";

或者使用 JSON 解析器来创建参数。

【讨论】:

  • @user1261404,可能是密码中的大写P。将其改为password...不过只是一个疯狂的猜测...
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 2013-08-08
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2021-05-29
相关资源
最近更新 更多