HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(URL)); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); String responseString = out.toString(); out.close(); //..more logic } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); }
public void post() throws Exception{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.baidu.com"); String xml = "<xml>xxxx</xml>"; HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8")); post.setEntity(entity); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity()); }
HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.example.com/" + URL); HttpResponse response = httpClient.execute(httpGet, localContext);
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do HttpResponse response = httpclient.execute(httpget); // Executeit HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); // Create an InputStream with the response BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) // Read line by line sb.append(line + "\n"); String resString = sb.toString(); // Result is here is.close(); // Close the stream
// Build the JSON object to pass parameters JSONObject jsonObj = new JSONObject(); jsonObj.put("username", username); jsonObj.put("apikey", apikey); // Create the POST object and add the parameters HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpPost);
<s> HttpClient httpClient = new DefaultHttpClient(); </s> //Deprecated HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead try { HttpPost request = new HttpPost("http://yoururl"); StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} "); request.addHeader("content-type", "application/x-www-form-urlencoded"); request.setEntity(params); HttpResponse response = httpClient.execute(request); // handle response here... }catch (Exception ex) { // handle exception here } finally { <s> httpClient.getConnectionManager().shutdown(); </s> //Deprecated }
try { final DefaultHttpClient client = new DefaultHttpClient(); final HttpPost httppost = new HttpPost("http://example.location.com/myxml"); final HttpResponse response = client.execute(httppost); final HttpEntity entity = response.getEntity(); final InputStream in = entity.getContent(); final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final XmlHandler handler = new XmlHandler(); parser.parse(in, handler); //TODO: get the data from your handler } catch (final Exception e) { Log.e("ParseError", "Error parsing xml", e); }
HttpHost proxy = new HttpHost("ip address",port number); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); HttpPost httpost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("param name", param)); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); System.out.println("Request Handled?: " + response.getStatusLine()); InputStream in = entity.getContent(); httpclient.getConnectionManager().shutdown();
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("LINK TO SERVER");
HttpClient httpclient; HttpPost httppost; ArrayList<NameValuePair> postParameters; httpclient = new DefaultHttpClient(); httppost = new HttpPost("your login link"); postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("param1", "param1_value")); postParameters.add(new BasicNameValuePair("param2", "param2_value")); httpPost.setEntity(new UrlEncodedFormEntity(postParameters)); HttpResponse response = httpclient.execute(httppost);
HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); String html = ""; InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while((line = reader.readLine()) != null) { str.append(line); } in.close(); html = str.toString();
public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } }
HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://foo.com/bar"); httpGet.addHeader(BasicScheme.authenticate( new UsernamePasswordCredentials("user", "password"), "UTF-8", false)); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity responseEntity = httpResponse.getEntity(); // read the stream returned by responseEntity.getContent()
JSONObject resultJson = new JSONObject(resultJsonString); String blobKey = resultJson.getString("blobKey"); String servingUrl = resultJson.getString("servingUrl"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("userId", userId)); nameValuePairs.add(new BasicNameValuePair("blobKey",blobKey)); nameValuePairs.add(new BasicNameValuePair("servingUrl",servingUrl)); HttpClient httpclient = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); // Continue to store the (immediately available) serving url in local storage f.ex
public static HttpResponse makeRequest(String path, Map params) throws Exception { //instantiates httpclient to make request DefaultHttpClient httpclient = new DefaultHttpClient(); //url with the post data HttpPost httpost = new HttpPost(path); //convert parameters into JSON object JSONObject holder = getJsonObjectFromMap(params); //passes the results to a string builder/entity StringEntity se = new StringEntity(holder.toString()); //sets the post request as the resulting string httpost.setEntity(se); //sets a request header so the page receving the request //will know what to do with it httpost.setHeader("Accept", "application/json"); httpost.setHeader("Content-type", "application/json"); //Handles what is returned from the page ResponseHandler responseHandler = new BasicResponseHandler(); return httpclient.execute(httpost, responseHandler); }
URL url=new URL(urlToHit); LoginScreen.httpClient = new DefaultHttpClient(); //LoginScreen is the name of the current Activity HttpPost httppost = new HttpPost(url.toString()); HttpResponse response = LoginScreen.httpClient.execute(httppost); xr.parse(new InputSource(url.openStream())); //SAX parsing
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://.../whatever"); List <NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("param1", "value1")); params.add(new BasicNameValuePair("param2", "value2")); ... httpost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httppost);
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); // ...
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder();
HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value")); nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String responseText = EntityUtils.toString(entity);