import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.http.impl.client.HttpClients; public class PostReqEx { public void sendReq(String url,String email,String fname){ HttpClient httpClient = HttpClients.createDefault(); PostMethod postMethod = new PostMethod(url); postMethod.addParameter("Email", email); postMethod.addParameter("fname", fname); try { httpClient.executeMethod(postMethod); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (postMethod.getStatusCode() == HttpStatus.SC_OK) { String resp = postMethod.getResponseBodyAsString(); } else { //...postMethod.getStatusLine(); } } }
/** * Add a schema to the controller. * @param schemaJson the json string representing the schema */ protected void addSchema(String schemaJson) throws IOException { String url = _controllerRequestURLBuilder.forSchemaCreate(); PostMethod postMethod = sendMultipartPostRequest(url, schemaJson); Assert.assertEquals(postMethod.getStatusCode(), 200); }
String url = _controllerRequestURLBuilder.forSchemaCreate(); PostMethod postMethod = sendMultipartPostRequest(url, schema.toString()); Assert.assertEquals(postMethod.getStatusCode(), 200); Assert.assertEquals(postMethod.getStatusCode(), 200);
@Override public void connect() throws ConnectionException { PostMethod post = new PostMethod(conn.getConnectUrl()); HttpClient httpClient = new HttpClient(); if (conn.getConnectUrl().toLowerCase().startsWith("https://")) { registerSsl(); } addPostHeaders(post); try { StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8"); post.setRequestEntity(requestEntity); httpClient.executeMethod(post); if (post.getStatusCode() != 200 && post.getStatusCode() != 201) { logger.error("Connect failed with error code " + post.getStatusCode() + " and message:\n" + post.getResponseBodyAsString()); throw new ConnectionException("Connect failed, error code " + post.getStatusCode() + " and message: " + post.getResponseBodyAsString()); } } catch (HttpException e) { logger.error(e.getLocalizedMessage(), e); throw new ConnectionException(e.getLocalizedMessage()); } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); throw new ConnectionException(e.getLocalizedMessage()); } }
response = post.getResponseBodyAsString(); if (post.getStatusCode() != 200 && post.getStatusCode() != 201) { logger.error("Failed to query", response); throw new SQLException(response);
protected boolean isRedirectRequired() { switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: return true; default: return false; } }
protected boolean isRedirectRequired() { switch (method.getStatusCode()) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: return true; default: return false; } }
public int getStatusCode() { return postMethod.getStatusCode(); }
@Override protected int getResponseCode() throws IOException { return postMethod.getStatusCode(); }
public int getStatusCode(){ return postMethod.getStatusCode(); } /**
@Override protected int getResponseCode() throws IOException { return postMethod.getStatusCode(); }
@Override protected int getResponseCode() throws IOException { return postMethod.getStatusCode(); }
int getHttpResponseCode() throws IOException { return postMethod != null ? postMethod.getStatusCode() : -1; }
@Override protected int getResponseCode() throws IOException { return postMethod.getStatusCode(); }
if (postMethod.getStatusCode() != HttpStatus.SC_CREATED) { String errorMessage = responseToErrorMessage(postMethod); postMethod.releaseConnection();
@Override public int getStatusCode() { if ("ErrorAccessDenied".equals(errorDetail)) { return HttpStatus.SC_FORBIDDEN; } else if ("ErrorItemNotFound".equals(errorDetail)) { return HttpStatus.SC_NOT_FOUND; } else { return super.getStatusCode(); } }
protected void checkStatusCode(PostMethod method) throws IOException { if (method.getStatusCode() != 200) { throw new IOException("Wrong status code: 200 expected but got " + method.getStatusCode() + " (" + method.getStatusText() + ")"); } }
String uri = "http://example.com/hotels/1/bookings"; PostMethod post = new PostMethod(uri); // create booking request content String request = post.setRequestEntity(new StringRequestEntity(request)); httpClient.executeMethod(post); if (HttpStatus.SC_CREATED == post.getStatusCode()) { Header location = post.getRequestHeader("Location"); if (location != null) { System.out.println("Created new booking at :" + location.getValue()); } }
public UserDetails getUserAttributes(String token) throws IOException, SingleSignOnException { HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(IDENT_ATTR_ENDPOINT); LOG.log(Level.INFO, "Obtaining user attributes: token: [" + token + "]: REST url: [" + IDENT_ATTR_ENDPOINT + "]"); NameValuePair[] data = { new NameValuePair("subjectid", token) }; post.setRequestBody(data); httpClient.executeMethod(post); if (post.getStatusCode() != HttpStatus.SC_OK) { throw new SingleSignOnException(post.getStatusLine().toString()); } return parseUserDetails(post.getResponseBodyAsString().trim()); }
public UserDetails getUserAttributes(String token) throws IOException, SingleSignOnException { HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(IDENT_ATTR_ENDPOINT); LOG.log(Level.INFO, "Obtaining user attributes: token: [" + token + "]: REST url: [" + IDENT_ATTR_ENDPOINT + "]"); NameValuePair[] data = { new NameValuePair("subjectid", token) }; post.setRequestBody(data); httpClient.executeMethod(post); if (post.getStatusCode() != HttpStatus.SC_OK) { throw new SingleSignOnException(post.getStatusLine().toString()); } return parseUserDetails(post.getResponseBodyAsString().trim()); }