【问题标题】:Java HTTP POST request string not arrivesJava HTTP POST 请求字符串未到达
【发布时间】:2017-01-05 15:00:17
【问题描述】:

当我调用 postFile() 时,HTTP 发布,但“请求”字符串没有到达服务器。主机收到请求并返回 400,这意味着在这种情况下请求为空。

每个例子都表明这应该足够了。

有什么问题?

public static void postFile(
    String host,
    String beaconId,
    String fileName,
    String base64File)
    throws IOException {

    StringBuilder request = new StringBuilder(base64File.length() + fileName.length() + 256);
    request.append("{\n");
    request.append("\"beaconId\": \"" + beaconId + "\",\n");
    request.append("\"fileName\": \"" + fileName + "\",\n");
    request.append("\"fileContent\": \"data:application/pdf;base64," + base64File + "\"\n");
    request.append("}");

    URL url = new URL("https://" + host + "/_ah/api/service/v1/files/add");
    //TODO ignore cert problems for now!
    TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {

            public X509Certificate[] getAcceptedIssuers() {

                return null;
            }

            public void checkClientTrusted(
                X509Certificate[] certs,
                String authType) {}

            public void checkServerTrusted(
                X509Certificate[] certs,
                String authType) {}
        }
    };
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("SSL");
    }
    catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    try {
        sslContext.init(null, trustAllCerts, new SecureRandom());
    }
    catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    try {
        conn.setSSLSocketFactory(sslContext.getSocketFactory());
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setDoOutput(true);
        try(OutputStream os = conn.getOutputStream()) {
            os.write(request.toString().getBytes("UTF-8"));
            os.flush();
            int respCode = conn.getResponseCode();
            String respMsg = conn.getResponseMessage();
            if (respCode != HTTP_CREATED) {
                throw new RuntimeException("HTTP error : " + conn.getResponseCode() + "\n" + respMsg);
            }
            String response = IOUtils.toString(conn.getInputStream(), "UTF-8");
            String[] lines = response.split("\n");
            if (lines.length != 6) {
                throw new RuntimeException("Invalid response : " + response);
            }
            if (!lines[2].contains("\"success\": true,")) {
                throw new RuntimeException("Post failed : " + response);
            }
        }
    }
    finally {
        conn.disconnect();
    }
} 

【问题讨论】:

    标签: java http post


    【解决方案1】:

    知道了!

    如果是 application/x-www-form-urlencoded 我必须使用

        request.append("beaconId=" + beaconId + "&");
        request.append("fileName=" + URLEncoder.encode(fileName, "UTF-8") + "&");
        request.append("fileContent=data:application/pdf;base64," + base64File);
    

    表格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-19
      • 2021-11-21
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多