1
\$\begingroup\$

I have a servlet deployed in a Tomcat Servlet Container in this link, http://localhost:9764/example/servlets/servlet/HelloWorldExample. I want to send a HTTP request to that servlet using Java. Just I want to get the return as a String. Currently I am using this code to do it, Is it OK to use this code?

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Client {
 public static void main(String[] args) {
 try {
 String webPage = "http://10.100.3.83:9764/example/servlets/servlet/HelloWorldExample";
 URL url = new URL(webPage);
 HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
 urlConnection.addRequestProperty("Name","andunslg");
 urlConnection.addRequestProperty("PW","admin");
 InputStream is = urlConnection.getInputStream();
 InputStreamReader isr = new InputStreamReader(is);
 int numCharsRead;
 char[] charArray = new char[1024];
 StringBuffer sb = new StringBuffer();
 while ((numCharsRead = isr.read(charArray)) > 0) {
 sb.append(charArray, 0, numCharsRead);
 }
 String result = sb.toString();
 System.out.println("*** BEGIN ***");
 System.out.println(result);
 System.out.println("*** END ***");
 } catch (MalformedURLException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
}
palacsint
30.3k9 gold badges82 silver badges157 bronze badges
asked Jun 19, 2012 at 3:52
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This appears ok. What problem you are facing ? \$\endgroup\$ Commented Jun 19, 2012 at 3:54
  • \$\begingroup\$ Not a problem. Just I want to verify that weather I am using much standard way to do it or is there any other better and efficient way to do it. \$\endgroup\$ Commented Jun 19, 2012 at 3:58

2 Answers 2

2
\$\begingroup\$

when you are sending any data to the servlet make sure it you send it via POST/GET depending on how sensitive the information is. In your case the user and password is sent through the get which is unsafe since it can be viewed by anyone.

you can add.urlConnection.setRequestMethod("POST"); Also to get the response you have to send the request first , which in your case is missing.

dataInput= new DataOutputStream(urlConnection.getOutputStream());
String content = "param1=" + URLEncoder.encode("first parameter") 
 + "&param2=" 
 + URLEncoder.encode("the second one...");
dataInput.writeBytes(content);
dataInput.flush();
dataInput.close();
answered Jun 19, 2012 at 4:08
\$\endgroup\$
1
\$\begingroup\$

+1 to @chaosguru, and instead of this:

InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
 sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();

Use Apache Commons IO's IOUtils.toString.

answered Jun 20, 2012 at 12:55
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.