List of usage examples for org.apache.http.impl.client DefaultHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:httpclient.client.ClientFormLogin.java
public static void main(String[] args) throws Exception { // prepare parameters HttpParams params = new BasicHttpParams(); HttpProtocolParams.setUserAgent(params, Constants.BROWSER_TYPE); HttpProtocolParams.setUseExpectContinue(params, true); DefaultHttpClient httpclient = new DefaultHttpClient(params); String loginURL = "http://10.1.1.251/login.asp"; HttpGet httpget = new HttpGet(loginURL); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { entity.consumeContent();//from ww w . j av a 2 s . co m } System.out.println("Initial set of cookies:"); List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } HttpPost httpost = new HttpPost(loginURL); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("name", "fangdj")); nvps.add(new BasicNameValuePair("passwd", "1111")); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpclient.execute(httpost); entity = response.getEntity(); for (Header h : response.getAllHeaders()) { System.out.println(h); } System.out.println("Login form get: " + response.getStatusLine()); if (entity != null) { entity.consumeContent(); } System.out.println("Post logon cookies:"); cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } String mainURL = "http://10.1.1.251/ONWork/Record_save.asp"; HttpPost mainHttPost = new HttpPost(mainURL); List<NameValuePair> mainNvps = new ArrayList<NameValuePair>(); mainNvps.add(new BasicNameValuePair("EMP_NO", "13028")); mainNvps.add(new BasicNameValuePair("pwd", "1111")); mainNvps.add(new BasicNameValuePair("cmd2", "Apply")); httpost.setEntity(new UrlEncodedFormEntity(mainNvps, HTTP.UTF_8)); response = httpclient.execute(mainHttPost); System.out.println(Tools.InputStreamToString(response.getEntity().getContent())); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:App.App.java
public static void main(String[] args) throws IOException { File inFile = new File("C:\\Data\\LogTest.java"); FileInputStream fis = new FileInputStream(inFile); DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost("http://localhost:8080/rest_j2ee/rest/files/upload"); MultipartEntity entity = new MultipartEntity(); entity.addPart("file", new InputStreamBody(fis, inFile.getName())); httppost.setEntity(entity);/*from ww w. ja va 2s . c om*/ HttpResponse response = httpclient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity, "UTF-8"); System.out.println("[" + statusCode + "] " + responseString); }
From source file:com.ds.test.ClientFormLogin.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*w w w.j ava 2 s . c o m*/ HttpGet httpget = new HttpGet("http://www.iteye.com/login"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("Login form get: " + response.getStatusLine()); System.out.println("cookies:"); List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println(cookies.get(i).toString()); } } /* HeaderIterator hi = response.headerIterator(); while(hi.hasNext()){ System.out.println(hi.next()); } EntityUtils.consume(entity); */ String token = parseHtml(EntityUtils.toString(entity)); httpget.releaseConnection(); System.out.println("********************************************************"); HttpPost httpost = new HttpPost("http://www.iteye.com/login"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("name", "")); nvps.add(new BasicNameValuePair("password", "")); nvps.add(new BasicNameValuePair("authenticity_token", token)); httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); response = httpclient.execute(httpost); entity = response.getEntity(); System.out.println("Login form get: " + response.getStatusLine()); EntityUtils.consume(entity); System.out.println("Post logon cookies:"); cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println(cookies.get(i).toString()); } } System.out.println("********************************************************"); HttpGet httpget2 = new HttpGet("http://www.iteye.com/login"); HttpResponse response2 = httpclient.execute(httpget2); HttpEntity entity2 = response2.getEntity(); System.out.println("Login form get: " + response2.getStatusLine()); print(response2); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:RestPostClient.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://localhost:10080/example/json/product/post"); StringEntity input = new StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}"); input.setContentType("application/json"); postRequest.setEntity(input);// w ww .j a va2 s . c om HttpResponse response = httpClient.execute(postRequest); //if (response.getStatusLine().getStatusCode() != 201) { // throw new RuntimeException("Failed : HTTP error code : " // + response.getStatusLine().getStatusCode()); //} BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); }
From source file:com.hilatest.httpclient.apacheexample.ClientAuthentication.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet("https://localhost/protected"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }//from w w w .jav a 2s . com if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:com.reversemind.glia.test.go3.java
public static void main(String... args) throws Exception { System.setProperty("http.proxyHost", "10.105.0.217"); System.setProperty("http.proxyPort", "3128"); System.setProperty("https.proxyHost", "10.105.0.217"); System.setProperty("https.proxyPort", "3128"); HttpHost proxy = new HttpHost("10.105.0.217", 3128); DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpGet request = new HttpGet( "https://twitter.com/i/profiles/show/splix/timeline/with_replies?include_available_features=1&include_entities=1&max_id=285605679744569344"); HttpResponse response = client.execute(request); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String sl = ""; String line = ""; while ((line = rd.readLine()) != null) { LOG.debug(line);/*from www.ja va 2 s . co m*/ sl += line; } sl = new String(sl.getBytes(), "UTF-8"); String sss = sl.replaceAll("\\{1,}", "\\").replaceAll("\\\"", "'").replaceAll("\\"", "'") .replaceAll(">", ">").replaceAll("<", "<").replaceAll("&", "&").replaceAll("'", "'") .replaceAll("\u003c", "<").replaceAll("\u003e", ">").replaceAll("\n", " ").replaceAll("\\/", "/") .replaceAll("\\'", "'"); String sss2 = sss.replaceAll("\\'", "'"); LOG.debug(sss); save("/opt/_del/go_sl.txt", sl); save("/opt/_del/go_sss.txt", sss); save("/opt/_del/go_line.txt", line); save("/opt/_del/go_sss2.txt", sss2); LOG.debug("\n\n\n\n\n"); LOG.debug(sss); LOG.debug("\n\n\n\n\n"); LOG.debug(URLDecoder.decode(sl, "UTF-8")); LOG.debug(URLDecoder.decode("\u0438\u043d\u043e\u0433\u0434\u0430", "UTF-8")); LOG.debug(URLDecoder.decode("\n \u003c/span\u003e\n \u003cb\u003e\n ", "UTF-8")); }
From source file:com.cloudhopper.httpclient.util.HttpsGetMain.java
static public void main(String[] args) throws Exception { ///*from ww w . j av a 2s.c om*/ // target urls // String strURL = "https://localhost:9443/"; DefaultHttpClient client = new DefaultHttpClient(); // setup this client to not verify SSL certificates HttpClientFactory.configureWithNoSslCertificateVerification(client); // add pre-emptive authentication HttpClientFactory.configureWithPreemptiveBasicAuth(client, "user0", "pass0"); HttpGet get = new HttpGet(strURL); HttpResponse httpResponse = client.execute(get); HttpEntity responseEntity = httpResponse.getEntity(); // // was the request OK? // if (httpResponse.getStatusLine().getStatusCode() != 200) { logger.error("Request failed with StatusCode=" + httpResponse.getStatusLine().getStatusCode()); } // get an input stream String responseBody = EntityUtils.toString(responseEntity); logger.debug("----------------------------------------"); logger.debug(responseBody); logger.debug("----------------------------------------"); client.getConnectionManager().shutdown(); }
From source file:com.dlmu.heipacker.crawler.client.ClientAuthentication.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*from w ww. j a v a 2 s . c o m*/ httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet("https://localhost/protected"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:servletPackage.ClientAuthentication.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); HttpGet httpget = new HttpGet( "http://localhost:8080/alfresco/d/a/workspace/SpacesStore/e80fb2c9-8468-45cd-b943-2d76ae13a260/epl-v10.html"); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }//ww w . j a va 2 s. c o m if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:com.quix.aia.cn.imo.mapper.UrlForUserData.java
public static void main(String[] args) { // TODO Auto-generated method stub UserAuthResponds userAuth = new UserAuthResponds(); try {/*from w ww . j a va 2 s .c o m*/ GsonBuilder builder = new GsonBuilder(); DefaultHttpClient httpClient = new DefaultHttpClient(); String username = "", psw = "", co = ""; username = "NSNP306"; psw = "A111111A"; co = "0986"; HttpGet getRequest = new HttpGet("http://211.144.219.243/isp/rest/index.do?isAjax=true&account=" + username + "&co=" + co + "&password=" + psw + ""); getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); Gson googleJson = new Gson(); userAuth = googleJson.fromJson(output, UserAuthResponds.class); System.out.println("Success " + userAuth.getSuccess()); if (userAuth.getSuccess().equals("1")) { System.out.println("Login successfully Done"); } else { System.out.println("Login Failed "); } } httpClient.getConnectionManager().shutdown(); // googleJson = builder.create(); // Type listType = new TypeToken<List<UserAuthResponds>>() {}.getType(); } catch (ClientProtocolException e) { log.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } catch (IOException e) { log.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } }