import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.cookie.CookiePolicy;
import java.io.IOException; public class Test4 { public static void main(String[] args) throws Exception { System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); System.setProperty("httpclient.useragent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); //create a singular HttpClient object String HOST ="localhost"; int PORT = 8080; HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(HOST, PORT, "http"); client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY ); String url = "test/jsp/index.jsp"; GetMethod getMethod = null; getMethod = new GetMethod(url); //getMethod.setFollowRedirects(true); //getMethod.setStrictMode(false); String responseBody = null; try{ client.executeMethod(getMethod); responseBody = getMethod.getResponseBodyAsString(); } catch (HttpException he) { System.err.println("Http error connecting to '" + url + "'"); System.err.println(he.getMessage()); System.exit(-4); } catch (IOException ioe){ System.err.println("Unable to connect to '" + url + "'"); System.exit(-3); } System.out.println("*** Request ***"); System.out.println("Request Path: " + getMethod.getPath()); System.out.println("Request Query: " + getMethod.getQueryString()); Header[] requestHeaders = getMethod.getRequestHeaders(); for (int i=0; i<requestHeaders.length; i++){ System.out.print(requestHeaders[i]); } System.out.println("*** Response ***"); System.out.println("Status Line: " + getMethod.getStatusLine()); Header[] responseHeaders = getMethod.getResponseHeaders(); for (int i=0; i<responseHeaders.length; i++){ System.out.print(responseHeaders[i]); } //write out the response body System.out.println("*** Response Body ***"); System.out.println(responseBody); //clean up the connection resources getMethod.releaseConnection(); getMethod.recycle(); NameValuePair[] formData = new NameValuePair[2]; formData[0] = new NameValuePair ("userId", "admin"); formData[1] = new NameValuePair ("password", "admin"); PostMethod postMethod = new PostMethod("/test/login.do"); postMethod.addParameters(formData); // postMethod.setFollowRedirects(true); //postMethod.setStrictMode(false); client.executeMethod(postMethod); responseBody = postMethod.getResponseBodyAsString(); System.out.println("Login form post: " + postMethod.getStatusLine().toString()); // release any connection resources used by the method Cookie[] logoncookies = client.getState().getCookies(HOST, PORT, "/", false); System.out.println("Logon cookies:"); if (logoncookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < logoncookies.length; i++) { System.out.println("- " + logoncookies[i].toString()); } } //write out the request headers System.out.println("*** Request ***"); System.out.println("Request Path: " + postMethod.getPath()); System.out.println("Request Query: " + postMethod.getQueryString()); requestHeaders = postMethod.getRequestHeaders(); for (int i=0; i<requestHeaders.length; i++){ System.out.print(requestHeaders[i]); } //write out the response headers System.out.println("*** Response ***"); System.out.println("Status Line: " + postMethod.getStatusLine()); responseHeaders = postMethod.getResponseHeaders(); for (int i=0; i<responseHeaders.length; i++){ System.out.print(responseHeaders[i]); } //write out the response body System.out.println("*** Response Body ***"); System.out.println(responseBody); System.out.println ("successfully logged in\n"); getMethod = new GetMethod("/test/home.do"); //getMethod.setFollowRedirects(true); //getMethod.setStrictMode(false); client.executeMethod(getMethod); responseBody = getMethod.getResponseBodyAsString(); //write out the request headers System.out.println("*** Request ***"); System.out.println("Request Path: " + getMethod.getPath()); System.out.println("Request Query: " + getMethod.getQueryString()); requestHeaders = getMethod.getRequestHeaders(); for (int i=0; i<requestHeaders.length; i++){ System.out.print(requestHeaders[i]); } //write out the response headers System.out.println("*** Response ***"); System.out.println("Status Line: " + getMethod.getStatusLine()); responseHeaders = getMethod.getResponseHeaders(); for (int i=0; i<responseHeaders.length; i++){ System.out.print(responseHeaders[i]); } //write out the response body System.out.println("*** Response Body ***"); System.out.println(responseBody); //clean up the connection resources getMethod.releaseConnection(); getMethod.recycle(); System.out.println("done"); System.exit(0); } } --- Saifadam Pathan <[EMAIL PROTECTED]> wrote: > Hi everyone, > > > can someone please help me to do the following: > > I wish to create a connection and send a request to > an website(using get or post ,etc methods). bUt most > importantly, i dont know is how do i form The header > and the parameters and etc... for the request and > then send it. > > Any help will be appreciated. > > best regards... > > Saif > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: > [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] ________________________________________________________________________ Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more. Go to: http://in.insurance.yahoo.com/licspecial/index.html --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]