Java url request builder

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A request builder for web applications completely based on java

License

derklaro/requestbuilder

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This repository provides a simple request builder for url connections completely based on the java provided classes without any external dependencies (main goal).

If you like the request builder and want to support our work you can star 🌟 and join our Discord.

Found a bug or have a proposal?

Please open an issue and describe the bug/proposal as detailed as possible and look into your email if we have replied to your issue and answer upcoming questions.

To include the project in yours you may use the following dependencies:

repository> id>jitpack.ioid> url>https://jitpack.iourl> repository>
dependency> groupId>com.github.derklarogroupId> artifactId>requestbuilderartifactId> version>1.0.6version> dependency>
maven < name 'jitpack.io' url 'https://jitpack.io' >
compile group: 'com.github.derklaro', name: 'requestbuilder', version: '1.0.5'
public class Main < public static void main(String[] args) < // Creates a simple builder with 'https://google.de' as target and no proxy RequestBuilder requestBuilder = RequestBuilder.newBuilder("https:google.de", Proxy.NO_PROXY); // We are now only accepting 'application/json' as result mime type requestBuilder.accepts(MimeTypes.getMimeType("json")); // We are sending the mime type 'application/json' requestBuilder.setMimeType(MimeTypes.getMimeType("json")); // We are following the redirects the server will make requestBuilder.enableRedirectFollow(); // We are now able to use the output stream of the connection requestBuilder.enableOutput(); // We are allowing the user to interact with the connection requestBuilder.enableUserInteraction(); // We will now bypass the caches of the jvm requestBuilder.disableCaches(); // We will now not accepting any incoming data requestBuilder.disableInput(); // After 5 seconds the connect should time out requestBuilder.setConnectTimeout(5, TimeUnit.SECONDS); // We will make a get request. Possibilities are get, post, head, options, put, delete, trace requestBuilder.setRequestMethod(RequestMethod.GET); // We are setting the maximum amount of time the client will read from the connection requestBuilder.setReadTimeOut(5, TimeUnit.SECONDS); // Sets the specified cookie during the request requestBuilder.addCookies(new HttpCookie("AName", "AValue")); // Adds a header to the connection requestBuilder.addHeader("AKey", "AValue"); // Adds a body to the request requestBuilder.addBody("AKey", "AValue"); try (RequestResult requestResult = requestBuilder.fireAndForget()) < // now we can handle the result of the connection > requestBuilder.fireAndForgetAsynchronously().thenAccept(result -> < // handle the result of the request >); > >
public class Main < public static void main(String[] args) < RequestResult requestResult = requestBuilder.fireAndForget(); // The status of the connection as an integer requestResult.getStatusCode(); // The status of the request as wrapped object requestResult.getStatus(); // Returns the result as string if the status code is == 200 else it will end up throwing an exception requestResult.getSuccessResultAsString(); // Returns the result as string if the status code is != 200 else it will end up throwing an exception requestResult.getErrorResultAsString(); // Returns always a string and decides between error or normal input requestResult.getResultAsString(); // Get all cookies the server has set requestResult.getCookies(); // Get all cookies the server has set in the specified header field requestResult.getCookies("MyHeader"); // Get the output stream which is direction -> to server requestResult.getOutputStream(); // Get the target input stream (direction -> to client) // Error -> returns the error stream (status != 200) // Default -> returns the default input stream (status == 200) // Choose -> chooses between error and default by result state requestResult.getStream(StreamType.CHOOSE); // If the connection to server has failed requestResult.hasFailed(); // If the client is still connected to server requestResult.isConnected(); // Closes the request and disconnects from the server try < requestResult.close(); > catch (Exception exception) < exception.printStackTrace(); > > >
git clone https://github.com/derklaro/requestbuilder.git cd requestbuilder/ mvn clean package 

Источник

Java url request builder

Sets the header named name to value . If this request already has any headers with that name, they are all replaced.

addHeader

public Request.Builder addHeader(String name, String value)

Adds a header with name and value . Prefer this method for multiply-valued headers like «Cookie». Note that for some headers including Content-Length and Content-Encoding , OkHttp may replace value with a header derived from the request body.

removeHeader

headers

cacheControl

public Request.Builder cacheControl(CacheControl cacheControl)

Sets this request’s Cache-Control header, replacing any cache control headers already present. If cacheControl doesn’t define any directives, this clears this request’s cache-control headers.

get

post

delete

public Request.Builder delete(@Nullable RequestBody body)

delete

put

patch

method

public Request.Builder method(String method, @Nullable RequestBody body)

tag

tag

public Request.Builder tag(Class type, @Nullable T tag)

Attaches tag to the request using type as a key. Tags can be read from a request using Request.tag() . Use null to remove any existing tag assigned for type . Use this API to attach timing, debugging, or other application data to a request so that you may read it in interceptors, event listeners, or callbacks.

build

Источник

Java url request builder

A builder of HTTP requests. Instances of HttpRequest.Builder are created by calling HttpRequest.newBuilder(URI) or HttpRequest.newBuilder() . The builder can be used to configure per-request state, such as: the request URI, the request method (default is GET unless explicitly set), specific request headers, etc. Each of the setter methods modifies the state of the builder and returns the same instance. The methods are not synchronized and should not be called from multiple threads without external synchronization. The build method returns a new HttpRequest each time it is invoked. Once built an HttpRequest is immutable, and can be sent multiple times. Note, that not all request headers may be set by user code. Some are restricted for security reasons and others such as the headers relating to authentication, redirection and cookie management may be managed by specific APIs rather than through directly user set headers.

Method Summary

Sets the request method of this builder to POST and sets its request body publisher to the given value.

Sets the request method of this builder to PUT and sets its request body publisher to the given value.

Method Detail

uri

expectContinue

HttpRequest.Builder expectContinue​(boolean enable)

Requests the server to acknowledge the request before sending the body. This is disabled by default. If enabled, the server is requested to send an error response or a 100 Continue response before the client sends the request body. This means the request publisher for the request will not be invoked until this interim response is received.

version

HttpRequest.Builder version​(HttpClient.Version version)

Sets the preferred HttpClient.Version for this request. The corresponding HttpResponse should be checked for the version that was actually used. If the version is not set in a request, then the version requested will be that of the sending HttpClient .

HttpRequest.Builder header​(String name, String value)

Adds the given name value pair to the set of headers for this request. The given value is added to the list of values for that name.

headers

Adds the given name value pairs to the set of headers for this request. The supplied String instances must alternate as header names and header values. To add several values to the same name then the same name must be supplied with each new value.

timeout

Sets a timeout for this request. If the response is not received within the specified timeout then an HttpTimeoutException is thrown from HttpClient::send or HttpClient::sendAsync completes exceptionally with an HttpTimeoutException . The effect of not setting a timeout is the same as setting an infinite Duration, ie. block forever.

setHeader

HttpRequest.Builder setHeader​(String name, String value)

Sets the given name value pair to the set of headers for this request. This overwrites any previously set values for name.

GET

POST

HttpRequest.Builder POST​(HttpRequest.BodyPublisher bodyPublisher)

Sets the request method of this builder to POST and sets its request body publisher to the given value.

PUT

HttpRequest.Builder PUT​(HttpRequest.BodyPublisher bodyPublisher)

Sets the request method of this builder to PUT and sets its request body publisher to the given value.

DELETE

method

HttpRequest.Builder method​(String method, HttpRequest.BodyPublisher bodyPublisher)

build

copy

Returns an exact duplicate copy of this Builder based on current state. The new builder can then be modified independently of this builder.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Читайте также:  Факториал числа си шарп
Оцените статью