Thursday, February 19, 2015

OAF Writing Data to a web page and retrieving result

I have a requirement where in I use a java class to connect to a webpage which is nothing but a query form and then use the result to update the tables in my application.

Logic.

xmlData = new StringBuffer("");
String formData = URLEncoder.encode("param1","UTF-8")+"=" URLEncoder.encode(param1,"UTF-8")+ param2+param3...

URL url = new URL(uri); //this uri is the url of the page I am using for querying..
URLCOnnection conn = url.openConnection();
conn.setDoOutput(true); //setDoOutput(true) is used for POST and PUT requests. If it is false then
                                        //it is for using GET requests.
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(formData);
writer.flush();
writer.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line = reader.readLine())!=null){
 xmlData.append(line); //this is where the whole data gets stored and it is to be processed later for  
                                      //extracting the needed data
}

 reader.close();

 

No comments:

Post a Comment