//Equivalent to previous example, but in a shorter way (using methods chaining): String json="{\"name\":\"Deadpool\",\"age\":40}";//JSON that we need to send String url="http://some.url";//URL address where we need to send it //Shortcut for example 5 complex request sending & reading response in one (chained) line JSONObject res=new HttpRequest(url).withHeaders("Content-Type: application/json").prepare(HttpRequest.Method.PUT).withData(json).sendAndReadJSON();
//Consider next request: HttpRequest req=new HttpRequest("http://host:port/path");
//send Http Post request to "http://url.com/b.c" in background using AsyncTask new AsyncTask<Void, Void, String>(){ protected String doInBackground(Void[] params) { String response=""; try { response=new HttpRequest("http://url.com/b.c").prepare(HttpRequest.Method.POST).sendAndReadString(); } catch (Exception e) { response=e.getMessage(); } return response; } protected void onPostExecute(String result) { //do something with response } }.execute();
//Downloading file byte [] file = new HttpRequest("http://some.file.url").prepare().sendAndReadBytes(); FileOutputStream fos = new FileOutputStream("smile.png"); fos.write(file); fos.close();
//Send Http PUT request to: "http://some.url" with request header: String json="{\"name\":\"Deadpool\",\"age\":40}";//JSON that we need to send String url="http://some.url";//URL address where we need to send it HttpRequest req=new HttpRequest(url);//HttpRequest to url: "http://some.url" req.withHeaders("Content-Type: application/json");//add request header: "Content-Type: application/json" req.prepare(HttpRequest.Method.PUT);//Set HttpRequest method as PUT req.withData(json);//Add json data to request body JSONObject res=req.sendAndReadJSON();//Accept response as JSONObject
someLongRunningHttpRequest = new HttpRequest("point://some/where") #cancel the request after 5 seconds setTimeout -> someLongRunningHttpRequest.cancel() , 5000
package com.dominikangerer.q29679878; import java.io.File; public class Main { public static void main(String[] args) { new HttpRequest("http://example.com/storeFile.php", "POST") .part("file", "file.jpg", new File("/your/file.jpg")) .part("path", "/the/path/it/should/be/stored/on/the/server/") .code(); } }
String contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; HttpRequest request = new HttpRequest("GET", new URI("https://examplebucket.s3.amazonaws.com?max-keys=2&prefix=J")); String signature = Signer.builder() .awsCredentials(new AwsCredentials(ACCESS_KEY, SECRET_KEY)) .header("Host", "examplebucket.s3.amazonaws.com") .header("x-amz-date", "20130524T000000Z") .header("x-amz-content-sha256", contentSha256) .buildS3(request, contentSha256) .getSignature();
public static void main(String[] args) throws Exception { final ServerSocket ss = new ServerSocket(8080); while (true) new HttpRequest(ss.accept()).run(); }
String contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; HttpRequest request = new HttpRequest("GET", new URI("https://examplebucket.s3.amazonaws.com?max-keys=2&prefix=J")); String signature = Signer.builder() .awsCredentials(new AwsCredentials(ACCESS_KEY, SECRET_KEY)) .header("Host", "examplebucket.s3.amazonaws.com") .header("x-amz-date", "20130524T000000Z") .header("x-amz-content-sha256", contentSha256) .buildS3(request, contentSha256) .getSignature();
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; URL url = new URL(YOUR_URL_PATH, "UTF-8")); ExecutorService executor = Executors.newFixedThreadPool(1); Future<HttpResponse> response = executor.submit(new HttpRequest(url)); executor.shutdown();
import java.io.*; import java.net.*; import java.util.*; public final class WebServer { public static void main(String args[]) throws Exception { int PORT = 8080; ServerSocket listenSocket = new ServerSocket(PORT); while(true) { HttpRequest request = new HttpRequest(listenSocket.accept()); Thread thread = new Thread(request); thread.start(); } } }
public void postData() { String fullUrl = "https://docs.google.com/forms/d/e/1FAIpQLSe5WZB-e95xrx3ZbwiOWtJx3wEuT-fJw/formResponse"; HttpRequest mReq = new HttpRequest(); String data = "entry.748835646=" + URLEncoder.encode(ed1.getText().toString()) + "&" + "entry.481985278=" + URLEncoder.encode(ed2.getText().toString()); String response = mReq.sendPost(fullUrl, data); Log.i(myTag, response); }
String fullUrl = "https://docs.google.com/forms/d/e/1FAIpQLSe5WZbIwv6m5uqf2Z73Uh7B-e95xrx3ZbEuT-fJw/formResponse"; HttpRequest mReq = new HttpRequest(); final EditText ed1 = (EditText)findViewById(R.id.location_address); final EditText ed2 = (EditText)findViewById(R.id.workername); location=ed1.getText().toString(); name=ed2.getText().toString(); String data = "entry.748835646=" + URLEncoder.encode(location) + "&" + "entry.481985278=" + URLEncoder.encode(name); String response = mReq.sendPost(fullUrl, data); Log.i(myTag, response); }
public void postData() { String fullUrl = "https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse"; HttpRequest mReq = new HttpRequest(); String response = mReq.sendPost(fullUrl, "entry.1566150510=data"); Log.i(myTag, response); }
HttpRequest request = new HttpRequest(4567); HttpResponse response = request.get("/test"); assertThat(response).hasStatusCode(201) .hasContentType("application/json") .hasBodyText("Hello World");
public class PostDataTask extends AsyncTask<String, Void, Integer>{ @Override protected Integer doInBackground(String... params) { HttpRequest mReq = new HttpRequest(); String data = "entry_272641491=" + URLEncoder.encode(params[1]) + "&" + "entry_130393492=" + URLEncoder.encode(params[2]); String response = mReq.sendPost(params[0], data); return 200; } }
final HttpRequest httpRequest = new HttpRequest(); new Thread(new Runnable() { @Override public void run() { String json; try { json = httpRequest.sendGet("url"); } catch (Exception e) { e.printStackTrace(); } } }).start();
Thread thread = new Thread() { @Override public void run() { try { // CALL YOUR FUNCTION FROM HERE HttpRequest r = new HttpRequest("http://www.hwlogos.com/test.json"); remoteViews.setTextViewText(R.id.ip, r.GetContent()); } catch (Exception e) { e.printStackTrace(); } } }; thread.start();
File f = new File("/path/fileToUpload.txt"); HttpRequest request = new HttpRequest("http://host/some_path"); Part[] parts = { new StringPart("param_name", "value"), new FilePart(f.getName(), f) }; filePost.setEntity( new MultipartRequestEntity(parts, filePost.getParams()) ); HttpClient client = new HttpClient(); int status = client.executeMethod(filePost);