List of usage examples for org.apache.http.protocol HTTP UTF_8
String UTF_8
To view the source code for org.apache.http.protocol HTTP UTF_8.
Click Source Link
From source file:de.chaosdorf.meteroid.longrunningio.LongRunningIOPost.java
@Override protected String doInBackground(Void... params) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try {// w w w. j a va 2s .com httpPost.setEntity(new UrlEncodedFormEntity(postData)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } try { HttpResponse response = httpClient.execute(httpPost, localContext); int code = response.getStatusLine().getStatusCode(); if (code >= 400 && code <= 599) { callback.displayErrorMessage(id, response.getStatusLine().getReasonPhrase()); return null; } HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity, HTTP.UTF_8); } catch (Exception e) { callback.displayErrorMessage(id, e.getLocalizedMessage()); return null; } }
From source file:com.intel.iotkitlib.LibHttp.HttpPutTask.java
private CloudResponse doWork(HttpClient httpClient, String url) { try {/* w ww .j a va 2 s. co m*/ HttpContext localContext = new BasicHttpContext(); HttpPut httpPut = new HttpPut(url); if (httpBody != null) { //setting HTTP body in entity StringEntity bodyEntity = new StringEntity(httpBody, "UTF-8"); httpPut.setEntity(bodyEntity); } //adding headers one by one for (NameValuePair nvp : headerList) { httpPut.addHeader(nvp.getName(), nvp.getValue()); } if (debug) { Log.e(TAG, "URI is : " + httpPut.getURI()); Header[] headers = httpPut.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Log.e(TAG, "Header " + i + " is :" + headers[i].getName() + ":" + headers[i].getValue()); } if (httpBody != null) { BufferedReader reader123 = new BufferedReader( new InputStreamReader(httpPut.getEntity().getContent(), HTTP.UTF_8)); StringBuilder builder123 = new StringBuilder(); for (String line = null; (line = reader123.readLine()) != null;) { builder123.append(line).append("\n"); } Log.e(TAG, "Body is :" + builder123.toString()); } } HttpResponse response = httpClient.execute(httpPut, localContext); if (response != null && response.getStatusLine() != null) { if (debug) Log.d(TAG, "response: " + response.getStatusLine().getStatusCode()); } HttpEntity responseEntity = response != null ? response.getEntity() : null; StringBuilder builder = new StringBuilder(); if (responseEntity != null) { BufferedReader reader = new BufferedReader( new InputStreamReader(responseEntity.getContent(), HTTP.UTF_8)); for (String line = null; (line = reader.readLine()) != null;) { builder.append(line).append("\n"); } if (debug) Log.d(TAG, "Response received is :" + builder.toString()); } CloudResponse cloudResponse = new CloudResponse(); if (response != null) { cloudResponse.code = response.getStatusLine().getStatusCode(); } cloudResponse.response = builder.toString(); return cloudResponse; } catch (java.net.ConnectException cEx) { Log.e(TAG, cEx.getMessage()); return null; } catch (Exception e) { Log.e(TAG, e.toString()); e.printStackTrace(); return null; } }
From source file:de.escidoc.core.test.sm.ReportDefinitionTestBase.java
/** * Test updating an reportDefinition of the mock framework. * * @param id The id of the reportDefinition. * @param xml The xml representation of the reportDefinition. * @return The updated report description. * @throws Exception If anything fails./*from w w w .j a v a2 s.com*/ */ @Override public String update(final String id, final String xml) throws Exception { Object result = getReportDefinitionClient().update(id, xml); String xmlResult = null; if (result instanceof HttpResponse) { HttpResponse httpRes = (HttpResponse) result; xmlResult = EntityUtils.toString(httpRes.getEntity(), HTTP.UTF_8); if (httpRes.getStatusLine().getStatusCode() >= 300 || httpRes.getStatusLine().getStatusCode() < 200) { throw new Exception(xmlResult); } } else if (result instanceof String) { xmlResult = (String) result; } return xmlResult; }
From source file:com.intel.iotkitlib.LibHttp.HttpPostTask.java
private CloudResponse doWork(HttpClient httpClient, String url) { try {/* w w w .j a v a2 s . c om*/ HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); if (httpBody != null) { //setting HTTP body in entity StringEntity bodyEntity = new StringEntity(httpBody, "UTF-8"); httpPost.setEntity(bodyEntity); } //adding headers one by one for (NameValuePair nvp : headerList) { httpPost.addHeader(nvp.getName(), nvp.getValue()); } if (debug) { Log.e(TAG, "URI is : " + httpPost.getURI()); Header[] headers = httpPost.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Log.e(TAG, "Header " + i + " is :" + headers[i].getName() + ":" + headers[i].getValue()); } if (httpBody != null) { BufferedReader reader123 = new BufferedReader( new InputStreamReader(httpPost.getEntity().getContent(), HTTP.UTF_8)); StringBuilder builder123 = new StringBuilder(); for (String line = null; (line = reader123.readLine()) != null;) { builder123.append(line).append("\n"); } Log.e(TAG, "Body is :" + builder123.toString()); } } HttpResponse response = httpClient.execute(httpPost, localContext); if (response != null && response.getStatusLine() != null) { if (debug) Log.d(TAG, "response: " + response.getStatusLine().getStatusCode()); } HttpEntity responseEntity = response != null ? response.getEntity() : null; StringBuilder builder = new StringBuilder(); if (responseEntity != null) { BufferedReader reader = new BufferedReader( new InputStreamReader(responseEntity.getContent(), HTTP.UTF_8)); for (String line = null; (line = reader.readLine()) != null;) { builder.append(line).append("\n"); } if (debug) Log.d(TAG, "Response received is :" + builder.toString()); } CloudResponse cloudResponse = new CloudResponse(); if (response != null) { cloudResponse.code = response.getStatusLine().getStatusCode(); } cloudResponse.response = builder.toString(); return cloudResponse; } catch (java.net.ConnectException cEx) { Log.e(TAG, cEx.getMessage()); return null; } catch (Exception e) { Log.e(TAG, e.toString()); e.printStackTrace(); return null; } }
From source file:utils.XMSEventListener.java
public void ConnectToXMS(String address, int port) { // If the Listener is already connected, the simply return if (isConnected) { return;/*w ww . j a v a2 s . c o m*/ } //Step 1: Create the Host host = new HttpHost(address, port, "http"); //Step 2: Create the XML payload for creation of and event handler. In this case will will be // listening for anyevent on any resource String xmlpayload = "<web_service version=\"1.0\">" + "<eventhandler>" + "<eventsubscribe type=\"any\" resource_id=\"any\" resource_type=\"any\"/>" + "</eventhandler>" + "</web_service>"; //HttpPost - HTTP POST method. // The HTTP POST method is defined in section 9.5 of RFC2616: // IN XMS REST Interface POST are used to create new event handler // http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpPost.html HttpPost httppost = new HttpPost("/default/eventhandlers?appid=app"); // The POST requires an XML payload. This block will convert the passed XML String to an http Entitiy and // attach it to the post message. Will also update the header to indicate the format of the payload is XML StringEntity se = null; try { se = new StringEntity(xmlpayload, HTTP.UTF_8); } catch (UnsupportedEncodingException ex) { Logger.getLogger(XMSEventListener.class.getName()).log(Level.SEVERE, null, ex); } httppost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httppost.setEntity(se); //Here you are issueing the POST Request, to the provided host via the HttpClient and storing // the response in the HpptResponse HttpResponse httpResponse = null; try { httpResponse = httpclient.execute(host, httppost); } catch (IOException ex) { Logger.getLogger(XMSEventListener.class.getName()).log(Level.SEVERE, null, ex); } //HttpEntity - An entity that can be sent or received with an HTTP message. // Entities can be found in some requests and in responses, where they are optional. // http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html HttpEntity entity = httpResponse.getEntity(); //This block will dump out the Status, headers and message contents if available System.out.println(httpResponse.getStatusLine()); Header[] headers = httpResponse.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(" " + headers[i]); } System.out.println("****** Message Contents *******"); String xmlresponse = ""; if (entity != null) { try { xmlresponse = EntityUtils.toString(entity); } catch (IOException ex) { Logger.getLogger(XMSEventListener.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(XMSEventListener.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(xmlresponse); } // Next we want to check to see if the request was successful by lookin for a 2xx response, // in this case it will be a 201 if (httpResponse.getStatusLine().getStatusCode() == 201) { System.out.println("XMSEventHandler Connected Successfully"); // If it is successful mark it as connected isConnected = true; //Here we need to pull out the href or callID Pattern pattern = Pattern.compile("href=\"(.*?)\""); Matcher matcher = pattern.matcher(xmlresponse); if (matcher.find()) { href = matcher.group(1); System.out.println("href=" + href); } else { System.out.println("No href found!"); } } }
From source file:jp.co.conit.sss.sp.ex1.util.SSSApiUtil.java
/** * ?????<br>/*from w w w. j av a 2 s . c o m*/ * ?????receipt?????<br> * ?????receipt?{@code null}???? * * @param context * @param productId * @param receipt * @return */ public static SPResult<List<DownloadFile>> getFileList(Context context, String productId, String receipt) { StringBuilder sbUrl = new StringBuilder(); sbUrl.append(DOMAIN); sbUrl.append("android/files/"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("token", Util.getToken(Util.getSignature(context)))); params.add(new BasicNameValuePair("token_method", "ndk")); params.add(new BasicNameValuePair("product_id", productId)); if (receipt != null) { params.add(new BasicNameValuePair("receipt", receipt)); } UrlEncodedFormEntity entity = null; try { entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String str = null; try { str = HttpUtil.post(sbUrl.toString(), entity); } catch (Exception e) { return SPResult.getSeverErrInstance(context); } return new DownloadFileListParser(context).getResult(str); }
From source file:com.groupme.sdk.util.HttpUtils.java
public static InputStream openStream(HttpClient client, String url, int method, String body, Bundle params, Bundle headers) throws HttpResponseException { HttpResponse response;/*from w w w . ja va 2 s . c om*/ InputStream in = null; Log.v("HttpUtils", "URL = " + url); try { switch (method) { case METHOD_GET: url = url + "?" + encodeParams(params); HttpGet get = new HttpGet(url); if (headers != null && !headers.isEmpty()) { for (String header : headers.keySet()) { get.setHeader(header, headers.getString(header)); } } response = client.execute(get); break; case METHOD_POST: if (body != null) { url = url + "?" + encodeParams(params); } HttpPost post = new HttpPost(url); Log.d(Constants.LOG_TAG, "URL: " + url); if (headers != null && !headers.isEmpty()) { for (String header : headers.keySet()) { post.setHeader(header, headers.getString(header)); } } if (body == null) { List<NameValuePair> pairs = bundleToList(params); post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)); } else { post.setEntity(new StringEntity(body)); } response = client.execute(post); break; case METHOD_PUT: if (body != null) { url = url + "?" + encodeParams(params); } HttpPut put = new HttpPut(url); if (headers != null && !headers.isEmpty()) { for (String header : headers.keySet()) { put.setHeader(header, headers.getString(header)); } } if (body == null) { List<NameValuePair> pairs = bundleToList(params); put.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)); } else { put.setEntity(new StringEntity(body)); } response = client.execute(put); break; default: throw new UnsupportedOperationException("Cannot execute HTTP method: " + method); } int statusCode = response.getStatusLine().getStatusCode(); if (statusCode > 400) { throw new HttpResponseException(statusCode, read(response.getEntity().getContent())); } in = response.getEntity().getContent(); } catch (ClientProtocolException e) { Log.e(Constants.LOG_TAG, "Client error: " + e.toString()); } catch (IOException e) { Log.e(Constants.LOG_TAG, "IO error: " + e.toString()); } return in; }
From source file:com.madrobot.net.HttpTaskHelper.java
/** * Access point to add the query parameter to the request url * /*from w w w . j av a 2 s. co m*/ * @throws URISyntaxException * if request url syntax is wrong */ private void addQueryParameter() throws URISyntaxException { this.requestUrl = URIUtils.createURI(this.requestUrl.getScheme(), this.requestUrl.getAuthority(), -1, this.requestUrl.getPath(), URLEncodedUtils.format(requestParameter, HTTP.UTF_8), null); }
From source file:com.googlecode.noweco.webmail.portal.bull.BullWebmailPortalConnector.java
private String authent(final DefaultHttpClient httpclient, final String user, final String password) throws IOException { // prepare the request HttpPost httpost = new HttpPost("https://bullsentry.bull.net:443/cgi/wway_authent?TdsName=PILX"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("Internet", "1")); nvps.add(new BasicNameValuePair("WebAgt", "1")); nvps.add(new BasicNameValuePair("UrlConnect", "https://telemail.bull.fr:443/")); nvps.add(new BasicNameValuePair("Service", "WEB-MAIL")); nvps.add(new BasicNameValuePair("Action", "go")); nvps.add(new BasicNameValuePair("lng", "EN")); nvps.add(new BasicNameValuePair("stdport", "80")); nvps.add(new BasicNameValuePair("sslport", "443")); nvps.add(new BasicNameValuePair("user", user)); nvps.add(new BasicNameValuePair("cookie", password)); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // send the request HttpResponse rsp = httpclient.execute(httpost); int statusCode = rsp.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Unable to connect to bull portail, status code : " + statusCode); }/* w w w. j a va 2 s. co m*/ if (LOGGER.isTraceEnabled()) { LOGGER.trace("STEP 1 Authent Cookie : {}", httpclient.getCookieStore().getCookies()); } // free result resources HttpEntity entity = rsp.getEntity(); String firstEntity = EntityUtils.toString(entity); if (entity != null) { LOGGER.trace("STEP 1 Entity : {}", firstEntity); EntityUtils.consume(entity); } return firstEntity; }
From source file:com.supremainc.biostar2.sdk.volley.toolbox.HttpClientStack.java
public HttpClient getNewHttpClient() { try {/* w ww . ja v a 2 s . c o m*/ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }