Java HTTP响应414

Java HTTP响应414,java,http-post,httpurlconnection,Java,Http Post,Httpurlconnection,我正在使用Java HttpURLConnection发送POST请求。我用方法1得到了414个响应 方法1: urlString = urlString + "?" + getParameters(params); 在方法1中,我将参数附加到URL。参数非常长,因此我得到了414响应代码 所以我尝试了下面的方法 方法2: 在这种方法中,我将参数作为表单数据发送。现在,服务器没有收到完整的数据或根本没有收到数据 URL url = new URL(urlString);

我正在使用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();
我试图在Java中执行这个curl命令

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

编辑2:

我尝试了ApacheHttpClient库

这是密码

        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());
            }
        }
HttpPost-HttpPost=new-HttpPost();
如果(标题!=null){
枚举枚举=(枚举)头。propertyNames();
while(enums.hasMoreElements()){
String key=enums.nextElement();
字符串val=headers.getProperty(键);
httpPost.addHeader(键,val);
}
addHeader(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=UTF-8”);
URI=新的URI(urlString);
setURI(uri);
如果(参数!=null){
迭代器it=params.keys();
MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
while(it.hasNext()){
String paramName=(String)it.next();
字符串paramValue=(字符串)params.get(paramName);
paramValue=URLDecoder.decode(paramValue);
entity.addPart(paramName,新的StringBody(paramValue,“text/plain”,Charset.forName(“UTF-8”));
}
httpPost.setEntity(实体);
}
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpResponse HttpResponse=httpClient.execute(httpPost);
if(httpresponse.getEntity()!=null){
StringBuilder sb=新的StringBuilder();
response=EntityUtils.toString(httpresponse.getEntity());
}
}

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

您不应该发送任何大字符串作为参数。你应该把它们作为邮件正文发送。然后可以写入请求输入流。服务器将接收它。此外,如果在URL字符串中包含任何内容,则必须对其进行适当的转义。这是一个很好的链接。如何发送post数据?请查看此链接。那么,我应该使用第三方库吗?这不能使用本机java httpurlconnection完成吗?
        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());
            }
        }