When to Use?

This is used when we want to access the any http or https url and then receive the data it sends back in java. Following is a sample code on how it can be used.

How to Use?
Replace the url you want to use and also parameters like param1, param2 which i used in the code to send parameters in encoded format. and then run...


I used it when I got into a situation where I need to send request to third party sever for fetching GPS related details. And the server responds back with a XML file. So, I should be capable enough to receive it and then parse it to get what exactly I wanted. I found this method very useful...

.......................................................................................................................................

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import javax.net.ssl.HttpsURLConnection;


public class http_https_connection extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";

public AtomDemo() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out=response.getWriter();
getPostData(request,response);
out.flush();
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.close();

}


private void getPostData(HttpServletRequest request,HttpServletResponse response){
String RevcodeData=null;
String content=null;
String address = null;
try{

response.setContentType("text/html");

URL url;
URLConnection urlConn;

url = new URL("http://any host name.com");

urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

// Send POST output.
cgiInput = new DataOutputStream(urlConn.getOutputStream());

content = "keyword=" + URLEncoder.encode("param1","UTF-8")
+ "&productName="+ URLEncoder.encode("param2","UTF-8");

cgiInput.writeBytes(content);
cgiInput.flush();
cgiInput.close();

BufferedReader cgiOutput =
new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
PrintWriter servletOutput = response.getWriter();
String line = null;
while (null != (line = cgiOutput.readLine())){
servletOutput.println(line);
System.out.println(line);
}
cgiOutput.close();

}catch (Exception e) {
// TODO: handle exception
}

}


public void init() throws ServletException {
// Put your code here
}

}

Comments (0)