List of usage examples for org.apache.commons.httpclient HttpStatus SC_OK
int SC_OK
To view the source code for org.apache.commons.httpclient HttpStatus SC_OK.
Click Source Link
From source file:com.interaction.example.odata.embedded.LinkIdITCase.java
@Test public void testLinkId() { try {//from w w w . jav a 2 s. com method = new GetMethod(baseUri + AIRPORTS); client.executeMethod(method); assertEquals(200, method.getStatusCode()); if (method.getStatusCode() == HttpStatus.SC_OK) { // read as string for debugging String response = method.getResponseBodyAsString(); Abdera abdera = new Abdera(); Parser parser = abdera.getParser(); Document<Feed> feedEntry = parser.parse(new StringReader(response)); Feed feed = feedEntry.getRoot(); List<Entry> entries = feed.getEntries(); assertEquals(6, entries.size()); // for(int i=0; i<entries.size(); i++) { // List<Link> link = entries.get(i).getLinks(); // for(int j=0; j<link.size(); j++) { // System.out.println(link.get(j).getAttributeValue("id")); // } // } List<Link> link = entries.get(0).getLinks(); assertNotNull(link); assertEquals("123456", link.get(0).getAttributeValue("id")); assertEquals("654321", link.get(1).getAttributeValue("id")); link = entries.get(1).getLinks(); assertNotNull(link); assertEquals("123456", link.get(0).getAttributeValue("id")); assertEquals("654321", link.get(1).getAttributeValue("id")); link = entries.get(2).getLinks(); assertNotNull(link); assertEquals("123456", link.get(0).getAttributeValue("id")); assertEquals("654321", link.get(1).getAttributeValue("id")); link = entries.get(3).getLinks(); assertNotNull(link); assertEquals("123456", link.get(0).getAttributeValue("id")); assertEquals("654321", link.get(1).getAttributeValue("id")); link = entries.get(4).getLinks(); assertNotNull(link); assertEquals("123456", link.get(0).getAttributeValue("id")); assertEquals("654321", link.get(1).getAttributeValue("id")); link = entries.get(5).getLinks(); assertNotNull(link); assertEquals("123456", link.get(0).getAttributeValue("id")); assertEquals("654321", link.get(1).getAttributeValue("id")); } } catch (IOException e) { fail(e.getMessage()); } finally { method.releaseConnection(); } }
From source file:net.neurowork.cenatic.centraldir.workers.xml.XmlTokenImporter.java
public String getXmlToken() { String token = null;/*from www . ja v a 2 s.c o m*/ HttpClient httpclient = XMLRestWorker.getHttpClient(); GetMethod get = new GetMethod(restUrl.getTokenUrl()); get.addRequestHeader("Accept", "application/xml"); NameValuePair userParam = new NameValuePair(XMLRestWorker.USERNAME_PARAM, satelite.getUser()); NameValuePair passwordParam = new NameValuePair(XMLRestWorker.PASSWORD_PARAM, satelite.getPassword()); NameValuePair[] params = new NameValuePair[] { userParam, passwordParam }; get.setQueryString(params); try { int statusCode = httpclient.executeMethod(get); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + get.getStatusLine()); } token = XMLRestWorker.parseToken(get.getResponseBodyAsString()); } catch (HttpException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } finally { get.releaseConnection(); } return token; }
From source file:com.appenginefan.toolkit.common.HttpClientEnvironment.java
@Override public String fetch(String data) { LOG.fine("sending " + data); PostMethod method = new PostMethod(url); method.setRequestBody(data);/*from w ww . ja va 2 s . com*/ try { int returnCode = client.executeMethod(method); if (returnCode == HttpStatus.SC_OK) { String response = method.getResponseBodyAsString(); LOG.fine("receiving " + response); return response; } else { LOG.log(Level.WARNING, "Communication failed, status code " + returnCode); } } catch (HttpException e) { LOG.log(Level.WARNING, "Communication failed ", e); } catch (IOException e) { LOG.log(Level.WARNING, "Communication failed ", e); } finally { method.releaseConnection(); } return null; }
From source file:com.owncloud.android.operations.OwnCloudServerCheckOperation.java
private boolean tryConnection(WebdavClient wc, String urlSt) { boolean retval = false; GetMethod get = null;//from ww w . j a va 2s .co m try { get = new GetMethod(urlSt); int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); String response = get.getResponseBodyAsString(); if (status == HttpStatus.SC_OK) { JSONObject json = new JSONObject(response); if (!json.getBoolean("installed")) { mLatestResult = new RemoteOperationResult( RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } else { mOCVersion = new OwnCloudVersion(json.getString("version")); if (!mOCVersion.isVersionValid()) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); } else { mLatestResult = new RemoteOperationResult( urlSt.startsWith("https://") ? RemoteOperationResult.ResultCode.OK_SSL : RemoteOperationResult.ResultCode.OK_NO_SSL); retval = true; } } } else { mLatestResult = new RemoteOperationResult(false, status); } } catch (JSONException e) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } catch (Exception e) { mLatestResult = new RemoteOperationResult(e); } finally { if (get != null) get.releaseConnection(); } if (mLatestResult.isSuccess()) { Log_OC.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); } else if (mLatestResult.getException() != null) { Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException()); } else { Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); } return retval; }
From source file:com.uber.jenkins.phabricator.FakeConduit.java
public void register(String method, JSONObject response) { server.register("/api/" + method, TestUtils.makeHttpHandler(HttpStatus.SC_OK, response.toString(2), requestBodies)); }
From source file:net.oauth.client.OAuthHttpClient.java
/** Send a message to the service provider and get the response. */ @Override//from www . j a v a 2 s . c om protected OAuthMessage invoke(OAuthMessage message) throws Exception { String form = OAuth.formEncode(message.getParameters()); HttpMethod method; if ("GET".equals(message.httpMethod)) { method = new GetMethod(message.URL); method.setQueryString(form); // method.addRequestHeader("Authorization", message // .getAuthorizationHeader(serviceProvider.userAuthorizationURL)); method.setFollowRedirects(false); } else { PostMethod post = new PostMethod(message.URL); post.setRequestEntity(new StringRequestEntity(form, OAuth.FORM_ENCODED, null)); method = post; } clientPool.getHttpClient(new URL(method.getURI().toString())).executeMethod(method); final OAuthMessage response = new HttpMethodResponse(method); int statusCode = method.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Map<String, Object> dump = response.getDump(); OAuthProblemException problem = new OAuthProblemException( (String) dump.get(OAuthProblemException.OAUTH_PROBLEM)); problem.getParameters().putAll(dump); throw problem; } return response; }
From source file:com.ifeng.vdn.ip.repository.service.impl.AliBatchIPAddressChecker.java
@Override public List<AliIPBean> check(List<String> ips) { List<AliIPBean> list = new ArrayList<AliIPBean>(); AliIPBean ipaddress = null;//w w w. j a v a 2 s . c o m String url = "http://ip.taobao.com/service/getIpInfo2.php"; for (String ip : ips) { // Create an instance of HttpClient. HttpClient clinet = new HttpClient(); // Create a method instance. PostMethod postMethod = new PostMethod(url); // Execute the method. try { postMethod.setParameter("ip", ip); int resultCode = clinet.executeMethod(postMethod); if (resultCode == HttpStatus.SC_OK) { // Read the response body. InputStream responseBody = postMethod.getResponseBodyAsStream(); ObjectMapper mapper = new ObjectMapper(); ipaddress = mapper.readValue(responseBody, AliIPBean.class); log.info(responseBody.toString()); list.add(ipaddress); } else { list.add(new AliIPBean()); log.error("Method failedd: [{}] , IP: [{}]", postMethod.getStatusCode(), ip); } } catch (JsonParseException | JsonMappingException | HttpException e) { list.add(new AliIPBean()); log.error(e.getMessage(), e); } catch (IOException e) { list.add(new AliIPBean()); log.error(e.getMessage(), e); } } return list; }
From source file:eu.eco2clouds.accounting.testbedclient.ClientHC.java
/** * Connects to an testbed URL to get the XML representations of Host Info * And it returns it as a null/*from w w w . jav a 2 s. c om*/ * @param testbed object representing the testbed from which we want to know the host info * @return A XML string with the Host info, if there was any HTTP error, it returns empty String... */ public String getHostInfo(Testbed testbed) { // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. GetMethod method = new GetMethod(testbed.getUrl()); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); String response = ""; try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { //TODO test for this case... logger.warn("Get host information of testbed: " + testbed.getName() + " failed: " + method.getStatusLine()); } else { // Read the response body. byte[] responseBody = method.getResponseBody(); response = new String(responseBody); } } catch (HttpException e) { logger.warn("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { logger.warn("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:com.owncloud.android.operations.ConnectionCheckOperation.java
private boolean tryConnection(WebdavClient wc, String urlSt) { boolean retval = false; GetMethod get = null;//from www .ja v a2s. co m try { get = new GetMethod(urlSt); int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); String response = get.getResponseBodyAsString(); if (status == HttpStatus.SC_OK) { JSONObject json = new JSONObject(response); if (!json.getBoolean("installed")) { mLatestResult = new RemoteOperationResult( RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } else { mOCVersion = new OwnCloudVersion(json.getString("version")); if (!mOCVersion.isVersionValid()) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); } else { mLatestResult = new RemoteOperationResult( urlSt.startsWith("https://") ? RemoteOperationResult.ResultCode.OK_SSL : RemoteOperationResult.ResultCode.OK_NO_SSL); retval = true; } } } else { mLatestResult = new RemoteOperationResult(false, status); } } catch (JSONException e) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } catch (Exception e) { mLatestResult = new RemoteOperationResult(e); } finally { if (get != null) get.releaseConnection(); } if (mLatestResult.isSuccess()) { Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); } else if (mLatestResult.getException() != null) { Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException()); } else { Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); } return retval; }
From source file:ehu.ned.DBpediaSpotlightClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;/*from www. j a v a 2s .co m*/ // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. // // Deal with the response. // // Use caution: ensure correct character encoding and is not binary data InputStream responseBody = method.getResponseBodyAsStream(); response = IOUtils.toString(responseBody, "UTF-8"); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }