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
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Sockets and Internet Protocols
Sending parameters from servlets to URL via POST
Arnob Dey
Ranch Hand
Posts: 43
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
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
Sheriff
Posts: 4644
582
I like...
posted 7 years ago
2
Number of slices to send:
Optional 'thank-you' note:
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
Posts: 43
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you very much Ron .... really appreciate it...
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
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...