@Override public String handleResponse( final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { LOGGER.error("Communication Error with ups quote " + status); throw new ClientProtocolException("UPS quote communication error " + status); } }
/** * 处理POST请求 * * @author https://github.com/yaphone * @date 2017年4月9日 下午7:06:35 * @param url * @param params * @return */ public HttpEntity doPost(String url, String paramsStr) { HttpEntity entity = null; HttpPost httpPost = new HttpPost(); try { StringEntity params = new StringEntity(paramsStr, Consts.UTF_8); httpPost = new HttpPost(url); httpPost.setEntity(params); httpPost.setHeader("Content-type", "application/json; charset=utf-8"); httpPost.setHeader("User-Agent", Config.USER_AGENT); CloseableHttpResponse response = httpClient.execute(httpPost); entity = response.getEntity(); } catch (ClientProtocolException e) { logger.info(e.getMessage()); } catch (IOException e) { logger.info(e.getMessage()); } return entity; }
HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url_select); httpPost.setEntity(new UrlEncodedFormEntity(param)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); e1.printStackTrace(); } catch (ClientProtocolException e2) { Log.e("ClientProtocolException", e2.toString()); e2.printStackTrace(); } catch (IllegalStateException e3) { Log.e("IllegalStateException", e3.toString()); BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8); StringBuilder sBuilder = new StringBuilder(); while ((line = bReader.readLine()) != null) { sBuilder.append(line + "\n");
try { HttpResponse httpResponse = httpClient.execute(httpPost); InputStream inputStream = httpResponse.getEntity().getContent(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String bufferedStrChunk = null; while((bufferedStrChunk = bufferedReader.readLine()) != null){ stringBuilder.append(bufferedStrChunk); } return stringBuilder.toString(); } catch (ClientProtocolException cpe) { System.out.println("First Exception caz of HttpResponese :" + cpe); cpe.printStackTrace(); } catch (IOException ioe) { System.out.println("Second Exception caz of HttpResponse :" + ioe); ioe.printStackTrace(); }
private static void handleResponse(CloseableHttpResponse response, OutputLogger logger) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException( statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); String tmp = reader.readLine(); while (tmp != null) { logger.log(tmp); tmp = reader.readLine(); } }
log.info("参数值:{}", jsonParams); HttpEntity httpEntity = new StringEntity(jsonParams, "utf-8"); httpPost.setEntity(httpEntity); HttpResponse httpResponse = defaultHttpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() != 200) { String errorLog="请求失败,errorCode:"+httpResponse.getStatusLine().getStatusCode(); log.info(errorLog); throw new Exception(url+errorLog); bufferedReader=new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"utf-8")); StringBuilder stringBuilder=new StringBuilder(); while ((output=bufferedReader.readLine())!=null){ stringBuilder.append(output); e.printStackTrace(); throw e; }catch (IOException e){ }finally { if(defaultHttpClient!=null) defaultHttpClient.getConnectionManager().shutdown(); if(bufferedReader!=null) bufferedReader.close();
public String communicate (String command) { httpClient = new DefaultHttpClient(); String url = "http://"+host+":"+port+"/console/command"; httpPost = new HttpPost(url); e.printStackTrace(err); } catch (ClientProtocolException e) { e.printStackTrace(err); } catch (IOException e) { e.printStackTrace(err); if(br!=null) { try { br.close(); } catch (IOException e) { ir.close(); } catch (IOException e) {
String jsonRequest = gson.toJson(nmsRequest); StringEntity input = new StringEntity(jsonRequest, ContentType.APPLICATION_JSON); HttpPost postRequest = new HttpPost(nmsUrl.toString()); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); final int status = response.getStatusLine().getStatusCode(); if (!isSuccess(status)) { throw new CloudRuntimeException("Failed on JSON-RPC API call. HTTP error code = " + status); try(BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));) { String tmp; while ((tmp = buffer.readLine()) != null) { sb.append(tmp); throw new CloudRuntimeException(ex.getMessage()); } catch (IOException ex) { throw new CloudRuntimeException(ex.getMessage()); if (httpClient != null) { try { httpClient.getConnectionManager().shutdown(); } catch (Exception t) { logger.debug(t.getMessage());
Scheme httpsScheme = new Scheme("https", sf, Properties.authzServerPort); HttpClient httpClient = new DefaultHttpClient(); httpClient.getConnectionManager().getSchemeRegistry().register(httpsScheme); postMethod = new HttpPost(Properties.oauthTokenEndPointURL); postMethod.setHeader("Content-Type", "application/x-www-form-urlencoded"); postMethod.setHeader("Authorization", "Basic " + authHeader); postMethod.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = httpClient.execute(postMethod); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); while ((line = rd.readLine()) != null) { result.append(line); return (String) jsonObject.get("access_token"); } catch (ClientProtocolException e) { throw new AiravataSecurityException(e.getMessage(), e); } catch (UnsupportedEncodingException e) { throw new AiravataSecurityException(e.getMessage(), e);
StringBuilder builder = new StringBuilder(); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setSoTimeout(httpParams, timeout); HttpClient client = new DefaultHttpClient(httpParams); String yourUrl = "http://www.ratemyplays.com/form.php"; HttpGet httpGet = new HttpGet(yourUrl); { try { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } String tempstr = builder.toString(); String str[] = tempstr.split(",");
@Override public DirectiveConfig handleResponse(HttpResponse response) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException( statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } Gson gson = new GsonBuilder().create(); Reader reader = new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")); DirectiveConfig config = gson.fromJson(reader, DirectiveConfig.class); return config; } }
HttpClient httpclient = new DefaultHttpClient(); String httppostURL = "http://192.168.0.254/nepadeals/androidweb/checkin"; HttpPost httppost = new HttpPost(httppostURL); Log.v(TAG, "postURL: " + httppost); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(content, HTTP.UTF_8)); String line; StringBuilder builder = new StringBuilder(); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Throwable t) { Toast.makeText(CheckinActivity.this, "Request failed: " + t.toString(),
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://localhost:8080/YourApp/YourAction"); try { List<NameValuePair> params = new ArrayList<NameValuePair>(1); params.add(new BasicNameValuePair("name", name)); post.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); // Server response as stream InputStream inputStream = entity.getContent(); } catch (ClientProtocolException cpe) { cpe.printStackTrace(); }
public String executePostRequest(String path, List<NameValuePair> params) { httpClient = new DefaultHttpClient(httpParameters); String ret = null; try { httpPost = new HttpPost(path); httpEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8); addHeader(httpPost); httpPost.setEntity(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = httpResponse.getEntity(); ret = EntityUtils.toString(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ret; }
@Override public PayDownloadfundflowResult handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); String str; dr.setData(str); Header headerDigest = response.getFirstHeader("Digest"); if (headerDigest != null) { String[] hkv = headerDigest.getValue().split("="); throw new ClientProtocolException("Unexpected response status: " + status);
HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, DEF_CONN_TIMEOUT); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, DEF_SOCKET_TIMEOUT); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); } catch (UnknownHostException e) { e.printStackTrace(); int httpCode = httpResponse.getStatusLine().getStatusCode(); if (httpCode == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { inputStream = httpEntity.getContent(); e.printStackTrace(); if (requestCallBack != null) { requestCallBack.onRequestError(
@Override public void notify(String message) { HttpClient httpClient = new DefaultHttpClient(); ProxyConfig proxyConfig = this.context.getProxyConfig("http"); if (null != proxyConfig) { HttpHost proxyHost = new HttpHost(proxyConfig.getHost(), proxyConfig.getPort()); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost); HttpPost httpPost = new HttpPost("http://www.spryng.be/send.php"); httpPost.setEntity(entity); httpResponse = httpClient.execute(httpPost); } catch (ClientProtocolException e) { LOG.error("client protocol error: " + e.getMessage()); return; } catch (IOException e) { return; StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); LOG.debug("status code: " + statusCode);
/** * @param sigdef */ public static void generatePRONOMSigFile(SigDefSubmission sigdef) { DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpPost httpost = new HttpPost(SERVICE_URL); httpost.setEntity(new UrlEncodedFormEntity( createNameValuePairs(sigdef), Consts.UTF_8)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); // Print out: IOUtils.copy(entity.getContent(), System.out); // Finish up: EntityUtils.consume(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
private static void sendPut(URI uri, byte[] body) { HttpClient httpClient = new DefaultHttpClient(); HttpPut httpPut = new HttpPut(uri); httpPut.setEntity(new ByteArrayEntity(body)); try { HttpResponse httpResponse = httpClient.execute(httpPut); if (httpResponse.getStatusLine().getStatusCode() == 200) { System.out.println("Send Put Success"); HttpEntity httpEntity = httpResponse.getEntity(); String response = EntityUtils.toString(httpEntity); System.out.println(response); } else { System.out.println(httpResponse.getStatusLine().getStatusCode()); System.out.println(httpResponse.getStatusLine().getReasonPhrase()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
public static void postReport(URL url, String param) throws IOException { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url.toExternalForm()); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("report", param)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request httpclient.execute(httppost); } catch (ClientProtocolException e) { throw new IOException(e.getMessage()); } }