HTTP响应414

时间:2018-07-27 14:23:05

标签: java http-post httpurlconnection

我正在使用Java HttpURLConnection发送POST请求。我使用方法1得到414响应

方法1:

urlString = urlString + "?" + getParameters(params);

在方法1中,我将参数附加到URL。参数很长,因此我得到了414响应代码。

所以我尝试了以下方法。

方法2:

在这种方法中,我将参数作为表单数据发送。现在,服务器没有收到完整的数据,或者根本没有收到数据。

        URL url = new URL(urlString);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setAllowUserInteraction(true);
        connection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);

        if(headers != null) {
            Enumeration<String> enums = (Enumeration<String>)headers.propertyNames();
            while(enums.hasMoreElements()) {
                String key = enums.nextElement();
                String val = headers.getProperty(key);

                connection.setRequestProperty(key, val);
            }
        }           

        writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
        if(params != null) {
            Iterator it = params.keys();
            while(it.hasNext()) {
                String name = (String)it.next();
                String value = (String)params.get(name);
                value = URLDecoder.decode(value);
                System.out.println(" Param Name :: " + name + " - Value :: " + value);

                writer.append("--" + boundary).append(LINE_FEED);
                writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
                writer.append("Content-Type: text/plain; charset=UTF-8").append(LINE_FEED);
                writer.append(LINE_FEED);
                writer.append(value).append(LINE_FEED);
                writer.flush();
            }
        }           

        if(fileDetails != null) {
            File dataFile = (File)fileDetails.get("FILE");
            String fileName = fileDetails.optString("FILE_NAME","");

            writer.append("--" + boundary).append(LINE_FEED);
            writer.append("Content-Disposition: form-data; name=\"" + "FILE" + "\";filename=\"" + fileName + "\"").append(LINE_FEED);
            writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(dataFile.getName()));
            writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
            writer.append(LINE_FEED);
            writer.flush();

            fis = new FileInputStream(dataFile);
            byte[] buffer = new byte[4096];
            int bytesRead = -1;
            while((bytesRead = fis.read(buffer)) != -1) {
                connection.getOutputStream().write(buffer, 0, bytesRead);
            }
            fis.close();

            writer.append(LINE_FEED); writer.flush();               
        }
        writer.append(LINE_FEED); writer.flush();
        writer.append("--" + boundary + "--").append(LINE_FEED); writer.close();

编辑:

等效卷曲

curl https://api.domain.com?param1=param1val
-X POST
-H "Authorization: authtoken ba4604e8e433gfda92e360d51263oec5"
-H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
-F 'JSONStr="{
    "key1": "val1",
    "key2": "val2",
    "key3": "val3"
   }"

我正在尝试用Java执行此curl命令。

如何使用Java在POST中发送大数据?我不会发送文件,而只是将JSONObject作为参数值中的字符串发送。

编辑2:

我尝试了Apache HttpClient库

这是代码。

        HttpPost httpPost = new HttpPost();
        if(headers != null) {
            Enumeration<String> enums = (Enumeration<String>)headers.propertyNames();
            while(enums.hasMoreElements()) {
                String key = enums.nextElement();
                String val = headers.getProperty(key);

                httpPost.addHeader(key, val);
            }
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            URI uri = new URI(urlString);
            httpPost.setURI(uri);

            if(params != null) {
                Iterator it = params.keys();
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                while(it.hasNext()) {
                    String paramName = (String)it.next();
                    String paramValue = (String)params.get(paramName);
                    paramValue = URLDecoder.decode(paramValue);

                    entity.addPart(paramName, new StringBody(paramValue, "text/plain", Charset.forName("UTF-8")));
                }
                httpPost.setEntity(entity);
            }

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse  httpresponse = httpClient.execute(httpPost);
            if(httpresponse.getEntity() != null) {
                StringBuilder sb = new StringBuilder();
                response = EntityUtils.toString(httpresponse.getEntity());
            }
        }

我尝试了上面的代码,但服务器未收到参数。

0 个答案:

没有答案