I've been searching for awhile on this topic. I'm trying to send data to my backend server using OkHttp. However, I am getting this error:
okhttp3.internal.http2.StreamResetException: stream was reset:PROTOCOL_ERROR
val client = OkHttpClient()
val response = client.newCall(
Request.Builder()
.addHeader("Authorization:", "Bearer $firebaseToken")
.url("https://someURL/"+ podcastId.toString())
.build())
.execute()
I'm not sure if this has to do with the URL or my Authorization header? Any help would be appreciated, thanks in advance.
I solved a similar problem by restricting the request to use HTTP 1, see:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
Related
I'm trying to understand how can I implement the use of a proxy for each request built like the following using Java API:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.version(HttpClient.Version.HTTP_2)
.uri(URI.createh("https://myurl"))
.timeout(Duration.ofMinutes(2))
.setHeader("User-Agent","Just an user agent")
.GET()
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
I'm seeing from the doc (https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html#Asynchronous%20Example)
that is possible with Synchronous requests. My code is within a method and it will run with threads parallelly. So how is it possible to set a proxy with Asynchronous Requests? If it is not possible, what's the difference between them?
Solved, it's a bit unclear the doc about that but at the end, I was able to set the proxy when building the client:
HttpClient client = HttpClient.newBuilder().
proxy(ProxySelector.of(new InetSocketAddress("proxy",port))))
.build();
//The request code is identical to what I wrote above.
The method is newBuilder anyway and not Builder.
I am trying to access Mindsphere URL with Java Code. I am getting 403 forbidden error while doing it. While I am able to hit other POST URL's for other sites, Mindsphere URL is getting blocked by same piece of Java Code. Can someone help?
What am i missing in my Code?
restTemplate.exchange(,,*,TimeseriesData.class) is line giving error
MindSphere demands a authorization header with a JWT Token, if you call directly the API. I guess you have an Developer account in MindSphere. Try Application credentials in the Developer cockpit. With that credentials you can get a bearer token with an oauth flow.
If not just ping me again.
See Exampel with OK HTTP
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url("https://questdev.piam.eu1.mindsphere.io/oauth/token")
.post(body)
.addHeader("Accept", "application/json")
.addHeader("cache-control", "no-cache,no-cache")
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Postman-Token", "24126d6b-3461-48fb-9060-6fd005804227")
.build();
Response response = client.newCall(request).execute();
We are using okhttp v3.8.0 in our project. We have to add custom header specifically for proxy server on https requests. The issues is that when i set ".header("Something", "FRR")", header would be encrypted on https requests as well, so it would not be identified by Proxy server. How can I achieve that? I want to send the header unencrypted in Initial method.
That's how I send my request to proxy server right now:
OK_HTTP_CLIENT = builder
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.cookieJar(cookieJar)
.retryOnConnectionFailure(true)
.proxy(proxy)
.proxyAuthenticator(proxyAuthenticatorMainAccount)
.build();
Request request = new Request.Builder()
.url(url)
.header("Something", "FRR")
.build();
Response response = OK_HTTP_CLIENT.newCall(request).execute();
There is screenshot here, explain what i want to achieve in more details
This isn't possibly in OkHttp currently. It's being tracked here. If it's important to you, please explain your situation and we’ll respond accordingly.
I`m writing some Rest client on Android and I met a problem - I have no idea how to make HEAD and OPTIONS requests.
There are no problems with GET/POST/PUT/DELETE/PATCH requests in OkHttp3, basically they looks like:
request = new Request.Builder()
.url(url)
.headers(headerBuilder.build())
.post(bodyBuilder.build())
.build();
And OkHttp3 doesnt provide additional methods like head() or option().
So how can I make HEAD and OPTIONS requests using OkHttp3?
Found answer, may be it will be useful for someone else
OkHttp3 still has method
Builder method(String method, RequestBody body)
So OPTIONS requests looks like
Request request = new Request.Builder()
.url(url)
.headers(headerBuilder.build())
.method("OPTIONS",requestBody)
.build();
same for HEAD
It appears (at least in the current implementation, API 3.12.0), HEAD request can be made just like GET and others:
Request request = new Request.Builder()
.url(url)
.head()
.build();
OPTION still has to be implemented using .method()
I'm trying to store an image to the Google Appengine Blobstore from an android device. What I've done so far:
Created an Enpoint (Google Cloud Endpoints) that returns an upload URL (Working)
Created a POST request with OKHTTP3 that sends the image file in a multipartform (Working? Maybe not?)
Created a Servlet that is passed to the upload URL to handle getting the keys. (It gets called, but getUpload always returns null.)
I'm thinking maybe it has to do with how I'm sending my POST request?
OkHttpClient client = new OkHttpClient();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"photo\""),
RequestBody.create(MediaType.parse("image/jpeg"), file)
)
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
In my Servlet I can see a param named "photo" but calling:
List<BlobKey> blobs = blobstoreService.getUploads(req).get("photo");
returns null. Zero BlobKeys...
I'm sure I'm missing something dumb... Any help would be incredibly appreciated!
So in the end it WAS the POST request.
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormData("photo", "photoname")
.addFormData("photo", "photo.jpg", RequestBody.create(MediaType.parse("image/jpeg"), file)
.build();