├── .github └── workflows │ └── Build-HTTPRequest.yml ├── .gitignore ├── LICENSE ├── README.md ├── SECURITY.md ├── pom.xml └── src ├── main └── java │ └── com │ └── konloch │ └── httprequest │ └── HTTPRequest.java └── test └── java └── com └── konloch └── HTTPRequestTest.java /.github/workflows/Build-HTTPRequest.yml: -------------------------------------------------------------------------------- 1 | # This will build the repo and upload the package as an artifact 2 | 3 | name: Build HTTPRequest 4 | 5 | on: 6 | push: 7 | branches: [ master ] 8 | pull_request: 9 | branches: [ master ] 10 | 11 | jobs: 12 | build: 13 | 14 | runs-on: ubuntu-latest 15 | strategy: 16 | matrix: 17 | java: [ '8', '11', '17' ] # LTS versions 18 | 19 | steps: 20 | - uses: actions/checkout@v3 21 | - name: Set up JDK ${{ matrix.java }} 22 | uses: actions/setup-java@v3 23 | with: 24 | java-version: ${{ matrix.java }} 25 | distribution: 'temurin' 26 | cache: maven 27 | - name: Build with Maven 28 | run: mvn -B package --file pom.xml 29 | - name: Extract Maven project version 30 | run: echo "lib_version=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)" >> $GITHUB_ENV 31 | id: project 32 | - name: 'Upload Artifact' 33 | uses: actions/upload-artifact@v3 34 | if: ${{ matrix.java == '8' }} 35 | with: 36 | name: HTTPRequest-${{ env.lib_version }}-SNAPSHOT 37 | path: target/HTTPRequest-${{ env.lib_version }}.jar 38 | retention-days: 90 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bin/ 2 | .idea/ 3 | .out/ 4 | /out 5 | .gradle/ 6 | .classpath 7 | .project 8 | .DS_Store 9 | *.iml 10 | /out/ 11 | /target/ 12 | 13 | dependency-reduced-pom.xml 14 | 15 | # Compiled class file 16 | *.class 17 | 18 | # Log file 19 | *.log 20 | 21 | # BlueJ files 22 | *.ctxt 23 | 24 | # Mobile Tools for Java (J2ME) 25 | .mtj.tmp/ 26 | 27 | # Package Files # 28 | *.jar 29 | *.war 30 | *.nar 31 | *.ear 32 | *.zip 33 | *.tar.gz 34 | *.rar 35 | 36 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 37 | hs_err_pid* 38 | replay_pid* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023 - ∞, Konloch 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HTTPRequest 2 | HTTPRequest is an easy-to-use zero dependency Java wrapper to read from a URL. 3 | 4 | Support for Cookies, proxies, UserAgent, post data and more. 5 | 6 | ## 💡 Requirements 7 | + Java Runtime 1.8 **or higher** 8 | 9 | ## ⚙️ How To Add As Library 10 | Add it as a maven dependency or just [download the latest release](https://github.com/Konloch/HTTPRequest/releases). 11 | ```xml 12 | 13 | com.konloch 14 | HTTPRequest 15 | 2.2.0 16 | 17 | ``` 18 | 19 | ## 📚 Links 20 | * [Website](https://konloch.com/HTTPRequest/) 21 | * [Discord Server](https://discord.gg/aexsYpfMEf) 22 | * [Download Releases](https://konloch.com/HTTPRequest/releases) 23 | 24 | ## 💻 How To Use 25 | **Simple Request:** 26 | ```java 27 | HTTPRequest request = new HTTPRequest(new URL("https://google.com/")); 28 | 29 | ArrayList webpage = request.read(); 30 | 31 | for(String line : webpage) 32 | System.out.println(line); 33 | ``` 34 | 35 | **Advanced Request:** 36 | ```java 37 | HTTPRequest request = new HTTPRequest(new URL("https://google.com/")); 38 | request.setTimeout(10000); 39 | request.setPostData("postdata=yes&awesome=yup"); 40 | request.setReferer("http://google.com/"); 41 | request.setCookie("cookies=yes;cool=sure"); 42 | request.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 81))); 43 | 44 | ArrayList webpage = request.read(); 45 | for(String line : webpage) 46 | System.out.println(line); 47 | 48 | for (Map.Entry> k : request.getLastConnectionHeaders()) 49 | System.out.println("Header Value:" + k.toString()); 50 | ``` 51 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | All versions are actively supported for security patches & updates. 6 | 7 | ## Reporting a Vulnerability 8 | 9 | E-Mail konloch@gmail.com if you find any issues. 10 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.konloch 5 | HTTPRequest 6 | 2.2.0 7 | 8 | HTTPRequest 9 | HTTPRequest is an easy-to-use zero dependency Java wrapper to read from a URL. Support for Cookies, proxies, UserAgent, post data and more. 10 | https://konloch.com/HTTPRequest/ 11 | 2012 12 | 13 | 14 | 15 | MIT License 16 | https://opensource.org/license/mit/ 17 | 18 | 19 | 20 | 21 | Konloch 22 | https://konloch.com 23 | 24 | 25 | 26 | 27 | Konloch 28 | konloch@gmail.com 29 | https://konloch.com 30 | Konloch 31 | https://konloch.com 32 | 33 | 34 | 35 | 36 | scm:git:git://github.com/Konloch/HTTPRequest.git 37 | scm:git:ssh://github.com:Konloch/HTTPRequest.git 38 | http://github.com/Konloch/HTTPRequest/tree/master 39 | 40 | 41 | 42 | 1.8 43 | ${java.version} 44 | ${java.version} 45 | UTF-8 46 | 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-compiler-plugin 53 | 3.10.1 54 | 55 | ${maven.compiler.source} 56 | ${maven.compiler.target} 57 | true 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-source-plugin 64 | 3.2.1 65 | 66 | 67 | attach-source 68 | 69 | jar 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-javadoc-plugin 78 | 3.4.0 79 | 80 | ${maven.compiler.source} 81 | 82 | 83 | 84 | attach-javadoc 85 | 86 | jar 87 | 88 | 89 | 90 | 91 | 92 | 93 | org.apache.maven.plugins 94 | maven-shade-plugin 95 | 3.3.0 96 | 97 | 98 | package 99 | 100 | shade 101 | 102 | 103 | 104 | 105 | 106 | 107 | com.konloch.httprequest.HTTPRequest 108 | 109 | ${project.version} 110 | ${maven.compiler.source} 111 | ${maven.compiler.target} 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /src/main/java/com/konloch/httprequest/HTTPRequest.java: -------------------------------------------------------------------------------- 1 | package com.konloch.httprequest; 2 | 3 | import java.io.*; 4 | import java.net.HttpURLConnection; 5 | import java.net.MalformedURLException; 6 | import java.net.Proxy; 7 | import java.net.URL; 8 | import java.nio.ByteBuffer; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.Map.Entry; 12 | import java.util.Set; 13 | 14 | /** 15 | * A simple wrapper for Java SE classes to write/read an HTTP Request 16 | * 17 | * @author Konloch 18 | * @since Jan 8, 2015 19 | */ 20 | public class HTTPRequest 21 | { 22 | public URL url; 23 | private int timeout = 30_000; 24 | private String cookie; 25 | private String referer; 26 | private String postData; 27 | private String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"; 28 | private Proxy proxy; 29 | private boolean setFollowRedirects = true; 30 | private InputStream stream; 31 | private BufferedReader reader; 32 | private DataOutputStream writer; 33 | private HttpURLConnection connection; 34 | private Set>> lastConnectionHeaders; 35 | private int lastStatusCode; 36 | 37 | /** 38 | * Creates a new HTTPRequest object 39 | * 40 | * @param url any valid URL path 41 | * @throws MalformedURLException if no protocol is specified, or an unknown protocol is found, or spec is null. 42 | */ 43 | public HTTPRequest(String url) throws MalformedURLException 44 | { 45 | this(new URL(url)); 46 | } 47 | 48 | /** 49 | * Creates a new HTTPRequest object 50 | * 51 | * @param url any supplied URL object 52 | */ 53 | public HTTPRequest(URL url) 54 | { 55 | this.url = url; 56 | } 57 | 58 | /** 59 | * Sets a referer to send in the HTTP request 60 | * 61 | * @param referer the referer that will be sent in the HTTP request 62 | * @return this instance for method chaining. 63 | */ 64 | public HTTPRequest setReferer(String referer) 65 | { 66 | this.referer = referer; 67 | return this; 68 | } 69 | 70 | /** 71 | * Set a cookie string to send in the HTTP request 72 | * 73 | * @param cookie the cookie that will be sent in the HTTP request 74 | * @return this instance for method chaining. 75 | */ 76 | public HTTPRequest setCookie(String cookie) 77 | { 78 | this.cookie = cookie; 79 | return this; 80 | } 81 | 82 | /** 83 | * Sets post data to send in the HTTP request 84 | * 85 | * @param postData the post data that will be sent in the HTTP request 86 | * @return this instance for method chaining. 87 | */ 88 | public HTTPRequest setPostData(String postData) 89 | { 90 | this.postData = postData; 91 | return this; 92 | } 93 | 94 | /** 95 | * Sets a custom UserAgent, default 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0' 96 | * 97 | * @param userAgent the UserAgent string 98 | * @return this instance for method chaining. 99 | */ 100 | public HTTPRequest setUserAgent(String userAgent) 101 | { 102 | this.userAgent = userAgent; 103 | return this; 104 | } 105 | 106 | /** 107 | * Sets the seconds till timeout, default 30,000 milliseconds 108 | * 109 | * @param timeout the timeout in milliseconds 110 | * @return this instance for method chaining. 111 | */ 112 | public HTTPRequest setTimeout(int timeout) 113 | { 114 | this.timeout = timeout; 115 | return this; 116 | } 117 | 118 | /** 119 | * Sets a proxy to connect through 120 | * 121 | * @param proxy 122 | * @return this instance for method chaining. 123 | */ 124 | public HTTPRequest setProxy(Proxy proxy) 125 | { 126 | this.proxy = proxy; 127 | return this; 128 | } 129 | 130 | /** 131 | * Follow redirects mean if a 302 or a 301 are encounted it will follow them. 132 | * 133 | * By default, follow redirects are enabled 134 | * 135 | * @param setFollowRedirects true enables follow redirects 136 | * @return this instance for method chaining. 137 | */ 138 | public HTTPRequest setFollowRedirects(boolean setFollowRedirects) 139 | { 140 | this.setFollowRedirects = setFollowRedirects; 141 | return this; 142 | } 143 | 144 | /** 145 | * Used to get the headers the webserver sent on our last connection 146 | * 147 | * NOTE: You can only get these headers after a read function has been called 148 | * 149 | * @return the last connection headers sent from the web server 150 | */ 151 | public Set>> getLastConnectionHeaders() 152 | { 153 | return lastConnectionHeaders; 154 | } 155 | 156 | /** 157 | * Used to get the last status code sent 158 | * 159 | * NOTE: You can only get these headers after a read function has been called 160 | * 161 | * @return the last status code sent from the web server 162 | */ 163 | public int getLastStatusCode() 164 | { 165 | return lastStatusCode; 166 | } 167 | 168 | /** 169 | * Read the first line from a remote URL and returns a single line 170 | * 171 | * @return the HTTP request contents as a String 172 | * @throws IOException if an I/O exception occurs. 173 | */ 174 | public String readSingle() throws IOException 175 | { 176 | return read(1).get(0); 177 | } 178 | 179 | /** 180 | * Reads from a remote URL and returns a String 181 | * 182 | * @return the HTTP request contents as a String 183 | * @throws IOException if an I/O exception occurs. 184 | */ 185 | public String readString() throws IOException 186 | { 187 | return String.join(File.separator, read()); 188 | } 189 | 190 | /** 191 | * Reads from a remote URL and returns a String Array 192 | * 193 | * @return the HTTP request contents as a String Array 194 | * @throws IOException if an I/O exception occurs. 195 | */ 196 | public String[] readArray() throws IOException 197 | { 198 | return readArray(-1); 199 | } 200 | 201 | /** 202 | * Reads from a remote URL and returns a String Array, optionally it can do a read for X lines 203 | * 204 | * @param linesToRead the amount of lines to read, -1 for unlimited 205 | * @return the HTTP request contents as a String Array 206 | * @throws IOException if an I/O exception occurs. 207 | */ 208 | public String[] readArray(int linesToRead) throws IOException 209 | { 210 | ArrayList lines = read(linesToRead); 211 | return lines.toArray(new String[lines.size()]); 212 | } 213 | 214 | /** 215 | * Reads from a remote URL and returns a String ArrayList 216 | * 217 | * @return the HTTP request contents as a String ArrayList 218 | * @throws IOException if an I/O exception occurs. 219 | */ 220 | public ArrayList read() throws IOException 221 | { 222 | return read(-1); 223 | } 224 | 225 | /** 226 | * Reads from a remote URL and returns a String ArrayList 227 | * 228 | * @param linesToRead the amount of lines to read, -1 for unlimited 229 | * @return the HTTP request contents as a String ArrayList 230 | * @throws IOException if an I/O exception occurs. 231 | */ 232 | public ArrayList read(int linesToRead) throws IOException 233 | { 234 | ArrayList lines; 235 | 236 | try { 237 | setup(); 238 | 239 | lines = new ArrayList<>(); 240 | String s; 241 | int counter = 0; 242 | while((s = reader.readLine()) != null) 243 | { 244 | lines.add(s); 245 | 246 | if(linesToRead > 0 && counter++ >= linesToRead) 247 | break; 248 | } 249 | 250 | lastConnectionHeaders = connection.getHeaderFields().entrySet(); 251 | lastStatusCode = connection.getResponseCode(); 252 | } catch(Exception e) { 253 | cleanup(); 254 | throw e; 255 | } finally { 256 | cleanup(); 257 | } 258 | 259 | return lines; 260 | } 261 | 262 | /** 263 | * Reads from a remote URL and returns a byte[] 264 | * 265 | * @return the HTTP request contents as a byte array 266 | * @throws IOException if an I/O exception occurs. 267 | */ 268 | public byte[] readBytes() throws IOException 269 | { 270 | ByteArrayOutputStream buffer; 271 | 272 | try { 273 | setup(); 274 | 275 | buffer = new ByteArrayOutputStream(); 276 | int read; 277 | while ((read = stream.read()) != -1) 278 | buffer.write((byte) read); 279 | 280 | lastConnectionHeaders = connection.getHeaderFields().entrySet(); 281 | lastStatusCode = connection.getResponseCode(); 282 | } catch(Exception e) { 283 | cleanup(); 284 | throw e; 285 | } finally { 286 | cleanup(); 287 | } 288 | 289 | return buffer.toByteArray(); 290 | } 291 | 292 | /** 293 | * Used to set up the connection to read the content. 294 | * @throws IOException if an I/O exception occurs. 295 | */ 296 | private void setup() throws IOException 297 | { 298 | if(proxy != null) 299 | connection = (HttpURLConnection) url.openConnection(proxy); 300 | else 301 | connection = (HttpURLConnection) url.openConnection(); 302 | 303 | if(cookie != null) 304 | connection.setRequestProperty("Cookie", cookie); 305 | if(referer != null) 306 | connection.addRequestProperty("Referer", referer); 307 | 308 | connection.setRequestProperty("User-Agent", userAgent); 309 | connection.setReadTimeout(timeout); 310 | connection.setConnectTimeout(timeout); 311 | connection.setUseCaches(false); 312 | HttpURLConnection.setFollowRedirects(setFollowRedirects); 313 | 314 | if(postData != null) { 315 | connection.setRequestMethod("POST"); 316 | connection.setDoOutput(true); 317 | connection.setDoInput(true); 318 | writer = new DataOutputStream(connection.getOutputStream()); 319 | writer.writeBytes(postData); 320 | writer.flush(); 321 | } 322 | 323 | reader = new BufferedReader(new InputStreamReader(stream = connection.getInputStream())); 324 | } 325 | 326 | /** 327 | * Used to clean up the connection, closes the connections and nulls the objects 328 | */ 329 | private void cleanup() { 330 | try { reader.close(); } catch(Exception e) {} 331 | try { writer.close(); } catch(Exception e) {} 332 | try { connection.disconnect(); } catch(Exception e) {} 333 | reader = null; 334 | writer = null; 335 | connection = null; 336 | } 337 | 338 | /** 339 | * Alert that this is a library 340 | * 341 | * @param args program launch arguments 342 | */ 343 | public static void main(String[] args) 344 | { 345 | throw new RuntimeException("Incorrect usage - for information on how to use this correctly visit https://konloch.com/HTTPRequest/"); 346 | } 347 | } -------------------------------------------------------------------------------- /src/test/java/com/konloch/HTTPRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.konloch; 2 | 3 | import com.konloch.httprequest.HTTPRequest; 4 | 5 | import java.util.ArrayList; 6 | 7 | /** 8 | * Used to test the API 9 | * 10 | * @author Konloch 11 | * @since 2/15/2023 12 | */ 13 | public class HTTPRequestTest 14 | { 15 | public static void main(String[] args) throws Exception 16 | { 17 | HTTPRequest request = new HTTPRequest("https://google.com"); 18 | 19 | ArrayList lines = request.read(); 20 | 21 | for(String line : lines) 22 | System.out.println(line); 23 | 24 | byte[] downloadTest = new HTTPRequest("https://google.com/favicon.ico").readBytes(); 25 | 26 | System.out.println(); 27 | System.out.println("Icon Size: " + downloadTest.length); 28 | } 29 | } 30 | --------------------------------------------------------------------------------