Java HTTP POST请求字符串未到达

时间:2017-01-05 15:00:18

标签: java http post

当我调用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();
    }
} 

1 个答案:

答案 0 :(得分:0)

知道了!

如果是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);

形式。