HTTP Response 414

Clash Royale CLAN TAG#URR8PPPHTTP Response 414
I'm sending a POST Request using Java HttpURLConnection. I got 414 Response using the Approach 1
Approach 1 :
urlString = urlString + "?" + getParameters(params);
In Approach 1, I append the parameters to the URL. The parameter is very long and hence I got 414 Response Code.
So I tried the below approach.
Approach 2 :
In this approach I sent the parameters as form data. Now, the server does not receive the complete data or does not receive the data at all.
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();
How do I send large data in POST using Java ? I'm not going to send Files, just JSONObject as a String in the parameter value.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.