Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Java
»
Sockets and Internet Protocols
Sending parameters from servlets to URL via POST
Arnob Dey
,
Ranch Hand
May 30, 2017 11:44:10
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
The task is pretty simple. We just need to send some parameter values to a listener port on an URL via POST from the
servlet
. But for some reasons I can't get it to work. Is there anything I missed or doing it wrong entirely?
public class SendToEMF extends HttpServlet { private static final long serialVersionUID = 1L; public SendToEMF() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); try { URL url = new URL("http://XXX.XXX.XXX.XXX:1111/"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty( "charset", "utf-8"); int responseCode = con.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) { String fileId = (String) request.getParameter("fileId"); String filename = (String) request.getParameter("filename"); String status = (String) request.getParameter("status"); String reason = (String) request.getParameter("reason"); String urlParams = "fileId="+fileId+"&filename="+filename+"&status="+status+"&reason="+reason; byte[] urlData = urlParams.getBytes(StandardCharsets.UTF_8); con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParams); wr.flush(); wr.close(); } } catch(Exception e) { e.printStackTrace(); } } }
Ron McLeod
,
Marshal
staff
May 30, 2017 12:26:07
(0 cow)
(0 skulls)
2
+Pie
Number of slices to send:
Send
The problem is that you are sending your HTTP request before adding your payload, resulting in a message
exchange
like this:
POST / HTTP/1.1 Content-Type: application/x-www-form-urlencoded charset: utf-8 Host: XXX.XXX.XXX.XXX:1111 HTTP/1.1 200 OK Content-Length: 0
You should
be calling
getResponseCode()
after setting your payload, which would result in a message exchange like this:
POST / HTTP/1.1 Content-Type: application/x-www-form-urlencoded charset: utf-8 Host: XXX.XXX.XXX.XXX:1111 Content-Length: 71 fileId=my-fileId&filename=my-filename&status=my-status&reason=my-reason HTTP/1.1 200 OK Content-Length: 0
For example, using your code:
URL url = new URL("http://XXX.XXX.XXX.XXX:1111/"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("charset", "utf-8"); String fileId = "my-fileId"; String filename = "my-filename"; String status = "my-status"; String reason = "my-reason"; String urlParams = "fileId=" + fileId + "&filename=" + filename + "&status=" + status + "&reason=" + reason; // byte[] urlData = urlParams.getBytes(StandardCharsets.UTF_8); con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParams); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); // <=== if (HttpURLConnection.HTTP_OK == responseCode) { System.out.println("Successful"); } else { System.out.println("Failed!"); }
Arnob Dey
,
Ranch Hand
May 31, 2017 00:07:52
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Thank you very much Ron .... really appreciate it...
Post by:
autobot
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
This thread has been viewed 643 times.
Similar Threads
java.lang.IllegalStateException: Already connected
How to create REST CLIENT for HTTPS in Java?
json Post methiod in Java
Send POST method by HttpURLConnection to an unknown devices
Http 405: The specified HTTP method is not allowed for the requested resource (Request method 'POST'
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:14:46
.