{"id":13452686,"url":"https://github.com/kevinsawicki/http-request","last_synced_at":"2025-05-14T02:04:51.992Z","repository":{"id":547583,"uuid":"2622727","full_name":"kevinsawicki/http-request","owner":"kevinsawicki","description":"Java HTTP Request Library","archived":false,"fork":false,"pushed_at":"2023-10-25T22:21:44.000Z","size":1678,"stargazers_count":3377,"open_issues_count":86,"forks_count":844,"subscribers_count":225,"default_branch":"master","last_synced_at":"2025-04-10T11:01:42.581Z","etag":null,"topics":["http","http-client","java"],"latest_commit_sha":null,"homepage":"http://kevinsawicki.github.io/http-request","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kevinsawicki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-10-21T19:54:58.000Z","updated_at":"2025-04-05T14:30:52.000Z","dependencies_parsed_at":"2024-06-18T16:52:24.303Z","dependency_job_id":"9a9ea1a2-6aaa-4a49-81a6-9e864ed9f21e","html_url":"https://github.com/kevinsawicki/http-request","commit_stats":{"total_commits":443,"total_committers":17,"mean_commits":"26.058823529411764","dds":0.04514672686230248,"last_synced_commit":"2d62a3e9da726942a93cf16b6e91c0187e6c0136"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinsawicki%2Fhttp-request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinsawicki%2Fhttp-request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinsawicki%2Fhttp-request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinsawicki%2Fhttp-request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kevinsawicki","download_url":"https://codeload.github.com/kevinsawicki/http-request/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254052670,"owners_count":22006716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","http-client","java"],"created_at":"2024-07-31T08:00:20.406Z","updated_at":"2025-05-14T02:04:51.947Z","avatar_url":"https://github.com/kevinsawicki.png","language":"Java","readme":"# Http Request [![Build Status](https://travis-ci.org/kevinsawicki/http-request.svg)](https://travis-ci.org/kevinsawicki/http-request)\n\nA simple convenience library for using a [HttpURLConnection](http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html)\nto make requests and access the response.\n\nThis library is available under the [MIT License](http://www.opensource.org/licenses/mit-license.php).\n\n## Usage\n\nThe http-request library is available from [Maven Central](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.github.kevinsawicki%22%20AND%20a%3A%22http-request%22).\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.github.kevinsawicki\u003c/groupId\u003e\n  \u003cartifactId\u003ehttp-request\u003c/artifactId\u003e\n  \u003cversion\u003e6.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nNot using [Maven](http://maven.apache.org/)? Simply copy the [HttpRequest](https://raw.githubusercontent.com/kevinsawicki/http-request/master/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java)\nclass into your project, update the package declaration, and you are good to go.\n\nJavadocs are available [here](http://kevinsawicki.github.com/http-request/apidocs/index.html).\n\n## FAQ\n\n### Who uses this?\n\nSee [here](https://github.com/kevinsawicki/http-request/wiki/Used-By) for a\nlist of known projects using this library.\n\n### Why was this written?\n\nThis library was written to make HTTP requests simple and easy when using a `HttpURLConnection`.\n\nLibraries like [Apache HttpComponents](http://hc.apache.org) are great but sometimes\nfor either simplicity, or perhaps for the environment you are deploying to (Android),\nyou just want to use a good old-fashioned `HttpURLConnection`.  This library seeks\nto add convenience and common patterns to the act of making HTTP requests such as\na fluid-interface for building requests and support for features such as multipart\nrequests.\n\n**Bottom line:** The single goal of this library is to improve the usability of the\n`HttpURLConnection` class.\n\n### What are the dependencies?\n\n**None**.  The goal of this library is to be a single class class with some inner static\nclasses.  The test project does require [Jetty](http://eclipse.org/jetty/) in order\nto test requests against an actual HTTP server implementation.\n\n### How are exceptions managed?\n\nThe `HttpRequest` class does not throw any checked exceptions, instead all low-level\nexceptions are wrapped up in a `HttpRequestException` which extends `RuntimeException`.\nYou can access the underlying exception by catching `HttpRequestException` and calling\n`getCause()` which will always return the original `IOException`.\n\n### Are requests asynchronous?\n\n**No**.  The underlying `HttpUrlConnection` object that each `HttpRequest`\nobject wraps has a synchronous API and therefore all methods on `HttpRequest`\nare also synchronous.\n\nTherefore it is important to not use an `HttpRequest` object on the main thread\nof your application.\n\nHere is a simple Android example of using it from an\n[AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html):\n\n```java\nprivate class DownloadTask extends AsyncTask\u003cString, Long, File\u003e {\n  protected File doInBackground(String... urls) {\n    try {\n      HttpRequest request =  HttpRequest.get(urls[0]);\n      File file = null;\n      if (request.ok()) {\n        file = File.createTempFile(\"download\", \".tmp\");\n        request.receive(file);\n        publishProgress(file.length());\n      }\n      return file;\n    } catch (HttpRequestException exception) {\n      return null;\n    }\n  }\n\n  protected void onProgressUpdate(Long... progress) {\n    Log.d(\"MyApp\", \"Downloaded bytes: \" + progress[0]);\n  }\n\n  protected void onPostExecute(File file) {\n    if (file != null)\n      Log.d(\"MyApp\", \"Downloaded file to: \" + file.getAbsolutePath());\n    else\n      Log.d(\"MyApp\", \"Download failed\");\n  }\n}\n\nnew DownloadTask().execute(\"http://google.com\");\n```\n\n## Examples\n\n### Perform a GET request and get the status of the response\n\n```java\nint response = HttpRequest.get(\"http://google.com\").code();\n```\n\n### Perform a GET request and get the body of the response\n\n```java\nString response = HttpRequest.get(\"http://google.com\").body();\nSystem.out.println(\"Response was: \" + response);\n```\n\n### Print the response of a GET request to standard out\n\n```java\nHttpRequest.get(\"http://google.com\").receive(System.out);\n```\n\n### Adding query parameters\n\n```java\nHttpRequest request = HttpRequest.get(\"http://google.com\", true, 'q', \"baseball gloves\", \"size\", 100);\nSystem.out.println(request.toString()); // GET http://google.com?q=baseball%20gloves\u0026size=100\n```\n\n### Using arrays as query parameters\n\n```java\nint[] ids = new int[] { 22, 23 };\nHttpRequest request = HttpRequest.get(\"http://google.com\", true, \"id\", ids);\nSystem.out.println(request.toString()); // GET http://google.com?id[]=22\u0026id[]=23\n```\n\n### Working with request/response headers\n\n```java\nString contentType = HttpRequest.get(\"http://google.com\")\n                                .accept(\"application/json\") //Sets request header\n                                .contentType(); //Gets response header\nSystem.out.println(\"Response content type was \" + contentType);\n```\n\n### Perform a POST request with some data and get the status of the response\n\n```java\nint response = HttpRequest.post(\"http://google.com\").send(\"name=kevin\").code();\n```\n\n### Authenticate using Basic authentication\n\n```java\nint response = HttpRequest.get(\"http://google.com\").basic(\"username\", \"p4ssw0rd\").code();\n```\n\n### Perform a multipart POST request\n\n```java\nHttpRequest request = HttpRequest.post(\"http://google.com\");\nrequest.part(\"status[body]\", \"Making a multipart request\");\nrequest.part(\"status[image]\", new File(\"/home/kevin/Pictures/ide.png\"));\nif (request.ok())\n  System.out.println(\"Status was updated\");\n```\n\n### Perform a POST request with form data\n\n```java\nMap\u003cString, String\u003e data = new HashMap\u003cString, String\u003e();\ndata.put(\"user\", \"A User\");\ndata.put(\"state\", \"CA\");\nif (HttpRequest.post(\"http://google.com\").form(data).created())\n  System.out.println(\"User was created\");\n```\n\n### Copy body of response to a file\n\n```java\nFile output = new File(\"/output/request.out\");\nHttpRequest.get(\"http://google.com\").receive(output);\n```\n### Post contents of a file\n\n```java\nFile input = new File(\"/input/data.txt\");\nint response = HttpRequest.post(\"http://google.com\").send(input).code();\n```\n\n### Using entity tags for caching\n\n```java\nFile latest = new File(\"/data/cache.json\");\nHttpRequest request = HttpRequest.get(\"http://google.com\");\n//Copy response to file\nrequest.receive(latest);\n//Store eTag of response\nString eTag = request.eTag();\n//Later on check if changes exist\nboolean unchanged = HttpRequest.get(\"http://google.com\")\n                               .ifNoneMatch(eTag)\n                               .notModified();\n```\n\n### Using gzip compression\n\n```java\nHttpRequest request = HttpRequest.get(\"http://google.com\");\n//Tell server to gzip response and automatically uncompress\nrequest.acceptGzipEncoding().uncompress(true);\nString uncompressed = request.body();\nSystem.out.println(\"Uncompressed response is: \" + uncompressed);\n```\n\n### Ignoring security when using HTTPS\n\n```java\nHttpRequest request = HttpRequest.get(\"https://google.com\");\n//Accept all certificates\nrequest.trustAllCerts();\n//Accept all hostnames\nrequest.trustAllHosts();\n```\n\n### Configuring an HTTP proxy\n\n```java\nHttpRequest request = HttpRequest.get(\"https://google.com\");\n//Configure proxy\nrequest.useProxy(\"localhost\", 8080);\n//Optional proxy basic authentication\nrequest.proxyBasic(\"username\", \"p4ssw0rd\");\n```\n\n### Following redirects\n\n```java\nint code = HttpRequest.get(\"http://google.com\").followRedirects(true).code();\n```\n\n### Custom connection factory\n\nLooking to use this library with [OkHttp](https://github.com/square/okhttp)?\nRead [here](https://gist.github.com/JakeWharton/5797571).\n\n```java\nHttpRequest.setConnectionFactory(new ConnectionFactory() {\n\n  public HttpURLConnection create(URL url) throws IOException {\n    if (!\"https\".equals(url.getProtocol()))\n      throw new IOException(\"Only secure requests are allowed\");\n    return (HttpURLConnection) url.openConnection();\n  }\n\n  public HttpURLConnection create(URL url, Proxy proxy) throws IOException {\n    if (!\"https\".equals(url.getProtocol()))\n      throw new IOException(\"Only secure requests are allowed\");\n    return (HttpURLConnection) url.openConnection(proxy);\n  }\n});\n```\n\n## Contributors\n\n* [Kevin Sawicki](https://github.com/kevinsawicki) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=kevinsawicki)\n* [Eddie Ringle](https://github.com/eddieringle) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=eddieringle)\n* [Sean Jensen-Grey](https://github.com/seanjensengrey) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=seanjensengrey)\n* [Levi Notik](https://github.com/levinotik) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=levinotik)\n* [Michael Wang](https://github.com/michael-wang) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=michael-wang)\n* [Julien HENRY](https://github.com/henryju) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=henryju)\n* [Benoit Lubek](https://github.com/BoD) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=BoD)\n* [Jake Wharton](https://github.com/JakeWharton) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=JakeWharton)\n* [Oskar Hagberg](https://github.com/oskarhagberg) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=oskarhagberg)\n* [David Pate](https://github.com/DavidTPate) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=DavidTPate)\n* [Anton Rieder](https://github.com/aried3r) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=aried3r)\n* [Jean-Baptiste Lièvremont](https://github.com/jblievremont) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=jblievremont)\n* [Roman Petrenko](https://github.com/romanzes) :: [contributions](https://github.com/kevinsawicki/http-request/commits?author=romanzes)\n","funding_links":[],"categories":["Java","Index","Libs","III. Network and Integration","网络编程","Programming Languages"],"sub_categories":["Networking","\u003cA NAME=\"Network\"\u003e\u003c/A\u003eNetwork","4. Http and ssh","Java"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevinsawicki%2Fhttp-request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkevinsawicki%2Fhttp-request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevinsawicki%2Fhttp-request/lists"}