public void post(String url, List<NameValuePair> nameValuePairs) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for(int index=0; index < nameValuePairs.size(); index++) { if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) { // If the key equals to "image", we use FileBody to transfer the data entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()))); } else { // Normal string data entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } } httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); } catch (IOException e) { e.printStackTrace(); } }
@Override protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception { HttpPost postMethod = new HttpPost(config.getServiceUrl()); ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray()); entity.setContentType(getContentType()); postMethod.setEntity(entity); BasicHttpContext context = null; if (environment.useSslContext()) { context = new BasicHttpContext(); context.setAttribute(HttpClientContext.USER_TOKEN, goAgentServerHttpClient.principal()); } try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod, context)) { validateResponse(response); InputStream responseBody = getResponseBody(response); return readRemoteInvocationResult(responseBody, config.getCodebaseUrl()); } }
public synchronized Object getAttribute(final String id) { return super.getAttribute(id); }
HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); httpget.setHeader("User-Agent", userAgent); HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == 200) { InputStream instream = entity.getContent(); String videoInfo = getStringFromInputStream(encoding, instream); HttpGet httpget2 = new HttpGet(downloadUrl); httpget2.setHeader("User-Agent", userAgent); log.finer("Executing " + httpget2.getURI()); HttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response2 = httpclient2.execute(httpget2); HttpEntity entity2 = response2.getEntity(); if (entity2 != null && response2.getStatusLine().getStatusCode() == 200) { long length = entity2.getContentLength();
public static String fetchThroughHttp(String address, String encoding) { try { org.apache.http.client.HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient(); org.apache.http.params.HttpParams params = httpClient.getParams(); org.apache.http.params.HttpConnectionParams.setConnectionTimeout(params, 10000); org.apache.http.params.HttpConnectionParams.setSoTimeout(params, 60000); org.apache.http.protocol.HttpContext localContext = new org.apache.http.protocol.BasicHttpContext(); org.apache.http.client.methods.HttpGet httpGet = new org.apache.http.client.methods.HttpGet(address); org.apache.http.HttpResponse response = httpClient.execute(httpGet, localContext); if (response.getStatusLine().getStatusCode() != 200) { return "FAILED"; } BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(encoding))); StringBuilder stringBuilder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } return stringBuilder.toString(); } catch (Exception e) { return "FAILED with exception: " + e.getMessage(); } }
HttpClient httpClient = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); // ... HttpResponse response1 = httpClient.execute(method1, httpContext); // ... HttpResponse response2 = httpClient.execute(method2, httpContext); // ...
public class ResquestJSON extends AsyncTask<String, Void, String>{ JSONObject jsonObject = new JSONObject(); String response; protected String doInBackground(String... params){ HttpPost httpPost = new HttpPost("http://192.168.10.101:8090/download");//your webservice url here HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); try { jsonObject.put("firstParam", params[0]); jsonObject.put("secondParam", params[1]); StringEntity se = new StringEntity(jsonObject.toString()); httpPost.addHeader("Content_Type", "application/json"); httpPost.setEntity(se); HttpResponse httpResponse = httpClient.execute(httpPost, httpContext); HttpEntity entity = httpResponse.getEntity(); if (entity!=null){ response = EntityUtils.toString(entity); entity.consumeContent(); httpClient.getConnectionManager().shutdown(); } } catch (JSONException|IOException|IllegalArgumentException e) { return UtilityConstants.ERROR; } return response; }
private static Map<String, Object> _httpGet(final String url, final String username, final String password) { HttpGet request = new HttpGet(url); Header authorizationHeader; try { authorizationHeader = scheme.authenticate(credentials, request, new BasicHttpContext()); } catch (AuthenticationException e) { throw new RuntimeException(e); request.addHeader(authorizationHeader); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { String body = EntityUtils.toString(entity); HashMap<String, String> m = new HashMap<String, String>(); for (Header h : response.getAllHeaders()) { m.put(h.getName(), h.getValue()); return ImmutableMap.of( "body", body, "statusCode", response.getStatusLine().getStatusCode(), "headers", headers); } else {
InputStream is = getAssets().open("contacts.csv"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(is); HttpContext localContext = new BasicHttpContext(); HttpResponse response =httpClient.execute(httpGet, localContext); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
HttpConnectionParams.setSoTimeout(myParams, 10000); httpClient = new DefaultHttpClient(myParams); localContext = new BasicHttpContext(); httpClient.getCookieStore().clear(); if (httpClient != null) { System.out.println("Abort."); httpPost.abort(); ret = null; httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); httpPost = new HttpPost(url); response = null; httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING HERE"); httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); response = httpClient.execute(httpPost,localContext); httpGet = new HttpGet(url);
public String httpGet(String url) throws IOException{ HttpGet get = new HttpGet(url); DefaultHttpClient() _httpClient = new DefaultHttpClient(); BasicHttpContext httpContext = new BasicHttpContext(); HttpResponse response=null; response = httpClient .execute(get, httpContext); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); return EntityUtils.toString(response.getEntity()); }
JSONArray json = //your array; HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost("http://your_url"); try { StringEntity se = new StringEntity(json.toString()); httpPost.setEntity(se); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse response = httpClient.execute(httpPost, httpContext); //execute your request and parse response HttpEntity entity = response.getEntity(); String jsonString = EntityUtils.toString(entity); //if response in JSON format } catch (Exception e) { e.printStackTrace(); }
HttpContext localContext = new BasicHttpContext(); String ret = null; HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); HttpPost httpPost = new HttpPost(url); HttpResponse response = null; StringEntity tmp = null; httpPost.setHeader( "Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); try { tmp = new StringEntity(data, "UTF-8"); } catch (UnsupportedEncodingException e) { Log.e("Your App Name Here", "HttpUtils : UnsupportedEncodingException : " + e); } httpPost.setEntity(tmp); try { response = httpClient.execute(httpPost, localContext); if (response != null) { ret = EntityUtils.toString(response.getEntity()); Log.i("result", ret); } } catch (Exception e) { Log.e("Your App Name Here", "HttpUtils: " + e); }
HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); // Prepare the cookie store to receive cookies. CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(csrfCookie); httpPost.setHeader("Referer", url); httpPost.setHeader("X-CSRFToken", csrfCookie.getValue()); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addTextBody("email", emailAddressToReset); httpPost.setEntity(builder.build()); HttpResponse httpResponse = httpClient.execute(httpPost, localContext); if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new Exception("Could not reset password!"); } Toast.makeText(context, "Password reset requested! Please check your email inbox!", Toast.LENGTH_LONG).show();
HttpResponse response; httpClient.getParams().setParameter("Your String", "YourStringValue"); localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(YOurURL); Log.e("URL", URL); //just to check httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109); StringEntity tmp = null; httpPost.setHeader("Accept", "application/json"); tmp = new StringEntity(inputJObject.toString(), "UTF-8"); httpPost.setEntity(tmp); response = httpClient.execute(httpPost, localContext); Log.i(TAG, response.getStatusLine().toString()); // Examine the response status HttpEntity entity = response.getEntity(); // Get hold of the response entity
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpget = new HttpGet("url"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new NTCredentials("username", "pwd", "", "domain")); List<String> authtypes = new ArrayList<String>(); authtypes.add(AuthPolicy.NTLM); httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,authtypes); localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider); HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity=response.getEntity();
HttpContext context = new BasicHttpContext(); CookieStore cookieStore = new BasicCookieStore(); context.setAttribute(ClientContext.COOKIE_STORE, cookieStore); int code = response.getStatusLine().getStatusCode(); String message = response.getStatusLine().getReasonPhrase(); String error = null; HttpEntity entity = response.getEntity(); HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); redirect = currentHost.toURI() + currentReq.getURI();
DefaultHttpClient client = new DefaultHttpClient(); HttpHost targetHost = new HttpHost("google.com", 80, "http"); HttpGet httpget = new HttpGet("about-us.html"); BasicHttpContext localContext = new BasicHttpContext(); HttpResponse response = null; try { response = client.execute(targetHost, httpget, localContext); System.out.println(response.getStatusLine() } catch(Exception e){ // Enter error-handling code here. }
HttpContext credContext = new BasicHttpContext(); credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp); HttpGet job = new HttpGet(url); HttpResponse response = httpClient.execute(host,job,credContext); StatusLine status = response.getStatusLine(); Log.d(TestActivity.TEST_TAG, status.toString()); httpClient.close();
// Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext);