List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString
public abstract String getResponseBodyAsString() throws IOException;
From source file:com.ebay.recommendations.RecommendationsSummary.java
/** * @param args//from w w w . ja va 2 s. co m */ public static void main(String[] args) { // TODO Auto-generated method stub HttpClient client = new HttpClient(); String fullUri = "https://svcs.ebay.com/services/selling/listingrecommendation/v1/item/recommendationsSummary"; HttpMethod method = new GetMethod(fullUri); // REPLACE YOUR TOKEN IN THE <YOUR_TOKEN_HERE> PLACE HOLDER method.addRequestHeader("Authorization", "TOKEN <YOUR_TOKEN_HERE>"); method.addRequestHeader("X-EBAY-GLOBAL-ID", "EBAY-US"); method.addRequestHeader("Accept", "application/xml"); try { client.executeMethod(method); String response = method.getResponseBodyAsString(); System.out.println(response); } catch (Exception e) { e.printStackTrace(); } }
From source file:TrivialApp.java
public static void main(String[] args) { if ((args.length != 1) && (args.length != 3)) { printUsage();//w w w . j a v a 2 s . c om System.exit(-1); } Credentials creds = null; if (args.length >= 3) { creds = new UsernamePasswordCredentials(args[1], args[2]); } //create a singular HttpClient object HttpClient client = new HttpClient(); //establish a connection within 5 seconds client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); //set the default credentials if (creds != null) { client.getState().setCredentials(AuthScope.ANY, creds); } String url = args[0]; HttpMethod method = null; //create a method object method = new GetMethod(url); method.setFollowRedirects(true); //} catch (MalformedURLException murle) { // System.out.println("<url> argument '" + url // + "' is not a valid URL"); // System.exit(-2); //} //execute the method String responseBody = null; try { client.executeMethod(method); responseBody = method.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); } //write out the request headers System.out.println("*** Request ***"); System.out.println("Request Path: " + method.getPath()); System.out.println("Request Query: " + method.getQueryString()); Header[] requestHeaders = method.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: " + method.getStatusLine()); Header[] responseHeaders = method.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 method.releaseConnection(); System.exit(0); }
From source file:ExampleP2PHttpClient.java
public static void main(String[] args) { // initialize JXTA try {//w w w. ja va 2 s .c om // sign in and initialize the JXTA network; profile this peer and create it // if it doesn't exist P2PNetwork.signin("clientpeer", "clientpeerpassword", "TestNetwork", true); } catch (Exception e) { e.printStackTrace(); System.exit(1); } // register the P2P socket protocol factory Protocol jxtaHttp = new Protocol("p2phttp", new P2PProtocolSocketFactory(), 80); Protocol.registerProtocol("p2phttp", jxtaHttp); //create a singular HttpClient object HttpClient client = new HttpClient(); //establish a connection within 50 seconds client.setConnectionTimeout(50000); String url = System.getProperty("url"); if (url == null || url.equals("")) { System.out.println("You must provide a URL to access. For example:"); System.out.println("ant example-webclient-run -D--url=p2phttp://www.somedomain.foo"); System.exit(1); } System.out.println("Connecting to " + url + "..."); HttpMethod method = null; //create a method object method = new GetMethod(url); method.setFollowRedirects(true); method.setStrictMode(false); //} catch (MalformedURLException murle) { // System.out.println("<url> argument '" + url // + "' is not a valid URL"); // System.exit(-2); //} //execute the method String responseBody = null; try { client.executeMethod(method); responseBody = method.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); } //write out the request headers System.out.println("*** Request ***"); System.out.println("Request Path: " + method.getPath()); System.out.println("Request Query: " + method.getQueryString()); Header[] requestHeaders = method.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: " + method.getStatusLine()); Header[] responseHeaders = method.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 method.releaseConnection(); method.recycle(); System.exit(0); }
From source file:Correct.java
public static void main(String[] args) { String URLL = ""; HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(URLL); method.setDoAuthentication(true);// w ww. j av a 2s .c o m HostConfiguration hostConfig = client.getHostConfiguration(); hostConfig.setHost("172.29.38.8"); hostConfig.setProxy("172.29.90.4", 8); // NTCredentials proxyCredentials = new NTCredentials("", "", "", ""); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", "")); //// try { //// URL url = new URL(""); //// HttpURLConnection urls = (HttpURLConnection) url.openConnection(); //// //// //// } catch (MalformedURLException ex) { //// Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex); //// } catch (IOException ex) { //// Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex); //// } try { // send the transaction client.executeMethod(hostConfig, method); StatusLine status = method.getStatusLine(); if (status != null && method.getStatusCode() == 200) { System.out.println(method.getResponseBodyAsString() + "\n Status code : " + status); } else { System.err.println(method.getStatusLine() + "\n: Posting Failed !"); } } catch (IOException ioe) { ioe.printStackTrace(); } method.releaseConnection(); }
From source file:com.discursive.jccook.httpclient.RedirectExample.java
private static void executeMethod(HttpClient client, HttpMethod method) throws IOException, HttpException { client.executeMethod(method);// w w w.j a v a2s .co m System.out.println("Response Code: " + method.getStatusCode()); String response = method.getResponseBodyAsString(); System.out.println(response); method.releaseConnection(); method.recycle(); }
From source file:com.cloud.test.utils.SubmitCert.java
public static void sendRequest(String url) { try {//from w w w. j a v a 2 s . c om HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); int responseCode = client.executeMethod(method); String is = method.getResponseBodyAsString(); s_logger.info("Response code " + responseCode + ": " + is); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:jshm.sh.Forum.java
public static void post(GameSeries series, PostMode mode, int postId, String subject, String body, boolean disableHtml, boolean disableSmilies, boolean attachSignature, boolean notifyOnReply) throws Exception { Client.getAuthCookies();//w w w .ja v a 2s . com String url = URLs.forum.getPostUrl(series, mode, postId); Client.makeHeadRequest(url); String[] staticData = { "subject", subject, "message", body, "mode", mode.value, "p", String.valueOf(postId), "sid", Client.getPhpBb2MySqlSid(), "post", "Submit" }; List<String> data = new ArrayList<String>(Arrays.asList(staticData)); if (disableHtml) { data.add("disable_html"); data.add("on"); } if (disableSmilies) { data.add("disable_smilies"); data.add("on"); } if (attachSignature) { data.add("attach_sig"); data.add("on"); } if (notifyOnReply) { data.add("notify"); data.add("on"); } new HttpForm((Object) url, data) { @Override public void afterSubmit(final int response, final HttpClient client, final HttpMethod method) throws Exception { String body = method.getResponseBodyAsString(); method.releaseConnection(); if (body.contains("Your message has been entered successfully.")) { LOG.fine("Posted successfully"); } else { int start = -1, end = -1; String needle = "<td align=\"center\"><span class=\"gen\">"; start = body.indexOf(needle); ClientException e = null; if (start >= 0) { start += needle.length(); needle = "</span></td>"; end = body.indexOf(needle, start); } else { needle = "<form "; start = body.indexOf(needle); needle = "</form>"; end = body.indexOf("</form>") + needle.length(); e = new ClientException("Unknown error while editing post"); } String msg = body.substring(start >= 0 ? start : 0, end >= 1 ? end : body.length()); LOG.finest("editPost() response:"); LOG.finest(msg); if (null == e) { e = new ClientException(msg); } LOG.throwing("Forum", "editPost", e); throw e; } } }.submit(); }
From source file:com.urswolfer.intellij.plugin.gerrit.rest.GerritApiUtil.java
@Nullable private static JsonElement request(@NotNull String host, @Nullable String login, @Nullable String password, @NotNull String path, @Nullable String requestBody, boolean post) { HttpMethod method = null; try {//www.j a v a2 s . c om method = doREST(host, login, password, path, requestBody, post); String resp = method.getResponseBodyAsString(); if (method.getStatusCode() != 200) { String message = String.format("Request not successful. Message: %s. Status-Code: %s.", method.getStatusText(), method.getStatusCode()); LOG.warn(message); throw new HttpStatusException(method.getStatusCode(), method.getStatusText(), message); } if (resp == null) { String message = String.format("Unexpectedly empty response: %s.", resp); LOG.warn(message); throw new RuntimeException(message); } return parseResponse(resp); } catch (IOException e) { LOG.warn(String.format("Request failed: %s", e.getMessage()), e); throw Throwables.propagate(e); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:com.flazr.util.Utils.java
public static String getOverHttp(String url) { HttpClient client = new HttpClient(); String response = null;// ww w .j av a 2 s . com HttpMethod get = new GetMethod(url); try { client.executeMethod(get); response = get.getResponseBodyAsString(); } catch (Exception e) { e.printStackTrace(); } finally { get.releaseConnection(); } return response; }
From source file:com.netflix.genie.server.util.NetUtil.java
/** * Returns the response from an HTTP GET call if it succeeds, null * otherwise.//from w ww .ja va2 s. c o m * * @param uri The URI to execute the HTTP GET on * @return response from an HTTP GET call if it succeeds, null otherwise * @throws IOException if there was an error with the HTTP request */ private static String httpGet(final String uri) throws IOException { String response = null; //TODO: Use one of other http clients to remove dependency final HttpClient client = new HttpClient(); final HttpMethod method = new GetMethod(uri); client.executeMethod(method); final int status = method.getStatusCode(); if (status == HttpURLConnection.HTTP_OK) { response = method.getResponseBodyAsString(); } return response; }