use of org.apache.http.entity.StringEntity in project OpenMEAP by OpenMEAP.
the class HttpRequestExecuterImpl method postContent.
public HttpResponse postContent(String url, String content, String contentType) throws HttpRequestException {
try {
StringEntity stringEntity = new StringEntity(content, FormConstants.CHAR_ENC_DEFAULT);
stringEntity.setContentType(contentType);
HttpPost httppost = new HttpPost(url);
httppost.setHeader(FormConstants.CONTENT_TYPE, contentType);
httppost.setEntity(stringEntity);
return execute(httppost);
} catch (Exception e) {
throw new HttpRequestException(e);
}
}
use of org.apache.http.entity.StringEntity in project FastDev4Android by jiangqqlmj.
the class BasicNetworkTest method headersAndPostParams.
@Test
public void headersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
fakeResponse.setEntity(new StringEntity("foobar"));
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return null;
}
@Override
protected void deliverResponse(String response) {
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestheader", "foo");
return result;
}
@Override
public Map<String, String> getParams() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestpost", "foo");
return result;
}
};
httpNetwork.performRequest(request);
assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
use of org.apache.http.entity.StringEntity in project hadoop by apache.
the class WebAppProxyServlet method proxyLink.
/**
* Download link and have it be the response.
* @param req the http request
* @param resp the http response
* @param link the link to download
* @param c the cookie to set if any
* @param proxyHost the proxy host
* @param method the http method
* @throws IOException on any error.
*/
private static void proxyLink(final HttpServletRequest req, final HttpServletResponse resp, final URI link, final Cookie c, final String proxyHost, final HTTP method) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY).setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
// Make sure we send the request from the proxy address in the config
// since that is what the AM filter checks against. IP aliasing or
// similar could cause issues otherwise.
InetAddress localAddress = InetAddress.getByName(proxyHost);
if (LOG.isDebugEnabled()) {
LOG.debug("local InetAddress for proxy host: {}", localAddress);
}
client.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
HttpRequestBase base = null;
if (method.equals(HTTP.GET)) {
base = new HttpGet(link);
} else if (method.equals(HTTP.PUT)) {
base = new HttpPut(link);
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
((HttpPut) base).setEntity(new StringEntity(sb.toString()));
} else {
resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
@SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
if (PASS_THROUGH_HEADERS.contains(name)) {
String value = req.getHeader(name);
if (LOG.isDebugEnabled()) {
LOG.debug("REQ HEADER: {} : {}", name, value);
}
base.setHeader(name, value);
}
}
String user = req.getRemoteUser();
if (user != null && !user.isEmpty()) {
base.setHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
}
OutputStream out = resp.getOutputStream();
try {
HttpResponse httpResp = client.execute(base);
resp.setStatus(httpResp.getStatusLine().getStatusCode());
for (Header header : httpResp.getAllHeaders()) {
resp.setHeader(header.getName(), header.getValue());
}
if (c != null) {
resp.addCookie(c);
}
InputStream in = httpResp.getEntity().getContent();
if (in != null) {
IOUtils.copyBytes(in, out, 4096, true);
}
} finally {
base.releaseConnection();
}
}
use of org.apache.http.entity.StringEntity in project ninja by ninjaframework.
the class NinjaTestBrowser method postJson.
public String postJson(String url, Object object) {
try {
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(object), "utf-8");
entity.setContentType("application/json; charset=utf-8");
post.setEntity(entity);
post.releaseConnection();
// Here we go!
return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.http.entity.StringEntity in project ninja by ninjaframework.
the class NinjaTestBrowser method postXml.
public String postXml(String url, Object object) {
try {
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(new XmlMapper().writeValueAsString(object), "utf-8");
entity.setContentType("application/xml; charset=utf-8");
post.setEntity(entity);
post.releaseConnection();
// Here we go!
return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations