List of usage examples for org.apache.http.message BasicHeader BasicHeader
public BasicHeader(String str, String str2)
From source file:com.momock.service.JsonService.java
@Override public void post(String url, String json, Header[] headers, final IEventHandler<JsonEventArgs> handler) { Logger.check(httpService != null, "The httpService must not be null!"); StringEntity entity = null;//www . j a va 2 s . c o m if (headers == null) headers = new Header[] { new BasicHeader(JSON_HEADER_KEY, JSON_HEADER_VAL) }; try { entity = new StringEntity(json, "UTF-8"); } catch (UnsupportedEncodingException e) { } final HttpSession session = httpService.post(url, headers, entity); session.getStateChangedEvent().addEventHandler(new IEventHandler<StateChangedEventArgs>() { @Override public void process(Object sender, StateChangedEventArgs args) { if (args.getState() == HttpSession.STATE_FINISHED) { JsonEventArgs a = new JsonEventArgs(session.getResultAsString(null), session.getError()); handler.process(session, a); } } }); session.start(); }
From source file:org.ardverk.daap.nio.DaapRequestReaderNIO.java
public DaapRequest read() throws IOException { DaapRequest ret = null;// w ww .j a v a2 s . c om if (pending.isEmpty() == false) ret = pending.removeFirst(); String line = null; while ((line = lineReader.read(in, connection.getReadChannel())) != null) { bytesRead += in.position(); if (requestLine == null) { requestLine = line; } else { int p = line.indexOf(':'); if (p == -1) { requestLine = null; headers.clear(); lineReader = null; throw new IOException("Malformed Header"); } String name = line.substring(0, p).trim(); String value = line.substring(++p).trim(); headers.add(new BasicHeader(name, value)); } } if (lineReader.isComplete()) { DaapRequest request = null; try { request = new DaapRequest(connection, requestLine); request.addHeaders(headers); } catch (URISyntaxException e) { IOException ioe = new IOException(); ioe.initCause(e); throw ioe; } finally { requestLine = null; headers.clear(); } if (ret == null) { ret = request; } else { pending.addLast(request); } } else if (headers.size() >= 64) { requestLine = null; headers.clear(); throw new IOException("Header too large"); } return ret; }
From source file:org.bishoph.oxdemo.util.CreateTaskAction.java
@Override protected JSONObject doInBackground(Object... params) { try {/*from www . j av a2s . c o m*/ String uri = (String) params[0]; String title = (String) params[1]; if (title == null && folder_id > 0) { Log.v("OXDemo", "Either title or folder_id missing. Done nothing!"); return null; } Log.v("OXDemo", "Attempting to create task " + title + " on " + uri); JSONObject jsonobject = new JSONObject(); jsonobject.put("folder_id", folder_id); jsonobject.put("title", title); jsonobject.put("status", 1); jsonobject.put("priority", 0); jsonobject.put("percent_completed", 0); jsonobject.put("recurrence_type", 0); jsonobject.put("private_flag", false); jsonobject.put("notification", false); Log.v("OXDemo", "Body = " + jsonobject.toString()); HttpPut httpput = new HttpPut(uri); StringEntity stringentity = new StringEntity(jsonobject.toString(), HTTP.UTF_8); stringentity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8")); httpput.setHeader("Accept", "application/json, text/javascript"); httpput.setHeader("Content-type", "application/json"); httpput.setEntity(stringentity); HttpResponse response = httpclient.execute(httpput, localcontext); Log.v("OXDemo", "Created task " + title); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity); Log.d("OXDemo", "<<<<<<<\n" + result + "\n\n"); JSONObject jsonObj = new JSONObject(result); jsonObj.put("title", title); return jsonObj; } catch (JSONException e) { Log.e("OXDemo", e.getMessage()); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.obm.sync.push.client.commands.Options.java
@Override public OptionsResponse run(AccountInfos ai, OPClient opc, HttpClient hc) throws Exception { HttpOptions request = new HttpOptions( opc.buildUrl(ai.getUrl(), ai.getLogin(), ai.getDevId(), ai.getDevType())); request.setHeaders(new Header[] { new BasicHeader("User-Agent", ai.getUserAgent()), new BasicHeader("Authorization", ai.authValue()) }); synchronized (hc) { try {/* www . j a va 2 s . c om*/ HttpResponse response = hc.execute(request); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { logger.error("method failed:{}\n{}\n", statusLine, response.getEntity()); } Header[] hs = response.getAllHeaders(); for (Header h : hs) { logger.info("resp head[" + h.getName() + "] => " + h.getValue()); } return new OptionsResponse(statusLine, ImmutableSet.copyOf(hs)); } finally { request.releaseConnection(); } } }
From source file:securitydigest.TestDigestScheme.java
public void testDigestAuthenticationWithNoRealm() throws Exception { Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, "Digest"); try {/*from www . j av a 2 s .c o m*/ AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); fail("Should have thrown MalformedChallengeException"); } catch (MalformedChallengeException e) { // expected } }
From source file:io.github.jonestimd.neo4j.client.http.ApacheHttpDriverTest.java
@Test public void post() throws Exception { BasicHeader header = new BasicHeader("", "header-value"); StringEntity responseEntity = new StringEntity("response entity"); when(client.execute(any(HttpUriRequest.class), any(HttpClientContext.class))).thenReturn(httpResponse); when(httpResponse.getFirstHeader(anyString())).thenReturn(null, header); when(httpResponse.getEntity()).thenReturn(responseEntity); HttpResponse response = driver.post(uri, "json entity"); verify(client).execute(postCaptor.capture(), same(context)); HttpPost post = postCaptor.getValue(); assertThat(post.getURI().toString()).isEqualTo(uri); assertThat(post.getEntity().getContentType().getValue()).isEqualTo(ContentType.APPLICATION_JSON.toString()); assertThat(getContent(post.getEntity().getContent())).isEqualTo("json entity"); assertThat(response.getHeader("header1")).isNull(); assertThat(response.getHeader("header2")).isEqualTo(header.getValue()); verify(httpResponse).getFirstHeader("header1"); verify(httpResponse).getFirstHeader("header2"); assertThat(getContent(response.getEntityContent())).isEqualTo("response entity"); response.close();//from w w w . j av a2 s. com verify(httpResponse).close(); }
From source file:monasca.common.middleware.HttpAuthClient.java
@Override public String validateTokenForServiceEndpointV3(String token) throws ClientProtocolException { String newUri = uri.toString() + "/v3/auth/tokens/"; Header[] header = new Header[] { new BasicHeader(AUTH_SUBJECT_TOKEN, token) }; return verifyUUIDToken(token, newUri, header); }
From source file:org.yaoha.ApiConnector.java
public ApiConnector() { String applicationVersion = ""; Context ctx = YaohaActivity.getStaticApplicationContext(); try {/*from www. j a v a 2s .c o m*/ applicationVersion = ctx.getApplicationContext().getPackageManager() .getPackageInfo(ctx.getPackageName(), 0).versionName; } catch (NameNotFoundException e) { applicationVersion = "version_unset"; } userAgentHeader = new BasicHeader("User-Agent", "YAOHA/" + applicationVersion + " (Android)"); setConsumer(); client = new DefaultHttpClient(); }
From source file:com.woonoz.proxy.servlet.UrlRewriterImpl.java
public String rewriteCookie(String headerValue) throws URISyntaxException, InvalidCookieException { BestMatchSpec parser = new BestMatchSpec(); List<Cookie> cookies;//from ww w. j ava 2 s .co m try { cookies = parser.parse(new BasicHeader("Set-Cookie", headerValue), new CookieOrigin( targetServer.getHost(), getPortOrDefault(targetServer), targetServer.getPath(), false)); } catch (MalformedCookieException e) { throw new InvalidCookieException(e); } if (cookies.size() != 1) { throw new InvalidCookieException(); } Cookie cookie = rewriteCookiePathIfNeeded(cookies.get(0)); CookieFormatter cookieFormatter = CookieFormatter.createFromApacheCookie(cookie); return cookieFormatter.asString(); }
From source file:eu.vranckaert.worktime.web.json.JsonWebServiceImpl.java
@Override public JsonResult webInvokePost(String baseUrl, String methodName, AuthorizationHeader authorizationHeader, Map<String, String> ampParams, JsonEntity jsonEntity, String... parameters) throws WebException, CommunicationException { String endpoint = baseUrl + methodName; if (parameters != null && parameters.length > 0) { for (Object param : parameters) { endpoint += "/" + param; }//from w w w. j a v a 2s . c o m } endpoint = buildEndpointWithAmpParams(endpoint, ampParams); httpPost = new HttpPost(endpoint); httpPost.setHeader(new BasicHeader(HTTP.CONTENT_ENCODING, "utf-8")); httpPost.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); if (authorizationHeader != null) { httpPost.setHeader("Authorization", authorizationHeader.getContent()); } if (jsonEntity != null) { String data = jsonEntity.toJSON(); try { StringEntity entity = new StringEntity(data, "utf-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(entity); } catch (UnsupportedEncodingException e) { } } HttpClient client = authorizationHeader == null ? getClient() : getNewClient(); try { HttpResponse response = client.execute(httpPost); return handleHttpResponse(response); } catch (UnknownHostException e) { throw new CommunicationException(e); } catch (IOException e) { throw new CommunicationException(e); } }