• Post Reply Bookmark Topic Watch Topic
  • New Topic

URLConnection class : Very Urgent

 
L.John
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
It would be kind of you if anyone could let me know how this problem could be solved.
I am taking a http Url from the database for eg. http://cou10.mydomain.co.uk/dnrs/rprfrservlet
I need to send an Http request to the above address.
What i need to do is use the URL and URL Connection class for this purpose because right now i jad been specifying the url path by hand, but it need to be fetched from the database.
The actual URL where the request is to be sent is : http://cou10.netbenefit.co.uk/dnrs/rprfrservlet
The follwoing is the existing code and now i have to use URL Connection class to implement the same functionality.
existing one.
-------------
String shost = "cou10.mydomain.co.uk";
int iport = 80;
clientsocket = new Socket(shost,iport);
InputStream in = clientsocket.getInputStream();
OutputStream out = clientsocket.getOutputStream();
String url = "GET /dnrs/rprfrservlet?name="+s_name+"&requestid="+lreq_id+"&status="+lRPStatus+"&msg="+encodedMessage+"&trackingid="+sTrackingId+"&date="+sDate+"&command="+sCommand+HttpVersion;
byte buf[] = url.getBytes();
out.write(buf);
out.flush();
logDebug("After getting the bytes ** "+new java.util.Date());
StringBuffer response = new StringBuffer();
int chr;

logDebug("Before the while loop " +new java.util.Date());
while((chr=in.read())!=-1) {
response.append((char)chr);
}

String sResponse = response.toString();
System.out.println(sResponse);

NOW I WANT TO USE THE URL CONNECTION CLASS. HOW CAN THE SAME BE IMPLEMENTED USING THE URLCONNECTION CLASS.
Kindly LET ME KNOW,
Many Thanks
John
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John
Since you are using HTTP protocol use HttpURLConnection class and its methods.
Hope this helps.
Regards
sanj
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Use
URL url = new URL("url from database?"+"your querystring");
This will create the URL with the query parameters that you need to send with the header.
you can use the url.openConnection() which will return the URLConnection class. You can access the input stream and output stream of this class using setDoInput(boolean) and setDoOutput(boolean).
regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!