|
|
|
This code will help you to send data (files, nvpair) on POST/GET method and to download any content from the web. You can also use proxy server while executing this.
Steps for installation...
1. Copy/extract the zip file attatched with this mail to jre/ext folder so world can access it OR make sure it is in the classpath while execution/compilation
2. create c:\temp folder (if you are lazy enough to modify the code)
3. run the example and you will see google logo in temp folder
Check out example steps for proxy settings. I have also included the POST data related stuff in it.
TODO: HTTPS example is yet to come... |
Click here to copy the following block | /* File main.java */ import HTTPClient.*;//the real don import java.io.*;//just for file io
/* If you are trying this example with proxy , you can do proxy setting by invoking HTTPConnection.setProxyServer("my.proxy.dom", 8008); Other typical things related to proxy setting are... 1 HTTPConnection.setProxyServer("my.proxy.dom", 8008); 2 HTTPConnection.dontProxyFor("localhost"); 3 HTTPConnection.dontProxyFor(".mycompany.com"); 4 AuthorizationInfo.addBasicAuthorization("my.proxy.dom", 8008, realm, user, passwd); */
public class main {
public main() { }
public static void main(String[] args) {
main main1 = new main(); String KeyWord=new String("myKeyWord");
//fetch an image from web ..u can try any mime like mp3 also System.out.println("Downloading logo file"); main1.getHttpData("www.google.com","/images/logo.gif","c:\\temp\\logo.gif");
//call GET method too.. for dynamic pages or sending the data System.out.println("Searching with the keyword!"); main1.getHttpData("www.foobar.com","/cgi-bin/search?q="+KeyWord,"c:\\temp\\result.html");
//send Data using POST method main1.PostData("www.foobar.com","/cgi-bin/sendata.cgi","c:\\temp\\result.html"); }
public void PostData(String HostFQDN,String PathAtHost,String LocalFileName){ try { NVPair form_data[] = new NVPair[2]; form_data[0] = new NVPair("name", "TheValueOfName"); form_data[1] = new NVPair("e-mail", "TheEmailAddress");
// note the convenience constructor for applets HTTPConnection con = new HTTPConnection(HostFQDN); HTTPResponse rsp = con.Post(PathAtHost, form_data); if (rsp.getStatusCode() >= 300) { System.err.println("Received Error: "+rsp.getReasonLine()); System.err.println(new String(rsp.getData())); } else{ FileOutputStream f = new FileOutputStream(LocalFileName); f.write(rsp.getData()); f.flush(); f.close(); } } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (ModuleException me) { System.err.println("Error handling request: " + me.getMessage()); } }
public void getHttpData(String HostFQDN,String PathAtHost,String LocalFileName){ byte[] data; try { HTTPConnection con = new HTTPConnection(HostFQDN); HTTPResponse rsp = con.Get(PathAtHost); if (rsp.getStatusCode() >= 300) { System.err.println("Received Error: "+rsp.getReasonLine()); System.err.println(new String(rsp.getData())); } else{ FileOutputStream f = new FileOutputStream(LocalFileName); f.write(rsp.getData()); f.flush(); f.close(); } } catch (IOException ioe) { System.err.println(ioe.toString()); } catch (ModuleException me) { System.err.println("Error handling request: " + me.getMessage()); } } } |
|
|
|
Submitted By :
Rathi Vijay
(Member Since : 6/5/2004 2:19:56 AM)
|
|
|
Job Description :
Hi, I persued my graduation in computer engineering. The area of technology which interests me much is, systems programming, mobile and wireless, embedded linux/RTOS.
At present (as on 6th June, 04) I am working with 3Di Systems (I) Pvt. Ltd., as Sr. Software Engineer in embedded linux/RTOS area. |
View all (4) submissions by this author
(Birth Date : 12/31/1979 ) |
|
|