HTTP(S) request to another server using Java tools? - java

I'm new to this and I need my Java program to send http or https requests on the different websites(for example, Amazon)
so it can get the HTML code and than I would be able to find information(price on some goods) that I need via Pattern class.
Did anybody faced with that task? Maybe you used JSON or other tools? Thank you.

It seems that Amazon have an API. You should using instead of trying to parse their website.
Regarding libs to call webservices in JAVA, you could use Retrofit.

There are several parts to what you are asking:
Constructing / determining what to include in the HTTP request
Issuing the actual HTTP request
Parsing the response
The first and last are dependent on the particular service / API you are invoking, though if the API response is in a standard format (e.g. JSON), there are libraries that can help you interpret the response (though exactly which fields in the response mean something to you, will depend on the particular API and your application). Issuing the HTTP request, itself, is something that can be done with a number of different libraries, including the builtin HttpURLConnection / URL classes, as well as third party libraries such as the Apache HttpComponents or the Google HTTP Java Client Library, the latter of which includes libraries for parsing common output formats, as well.

Related

Docusign: creating envelope with multiple documents in multipart request from Java

The Docusign Java SDK doesn't have built-in support for creating envelopes with multiple documents as binary. This is cumbersome for users. People asking about have been directed to this one-off example code using Java's native HttpsUrlConnection: Docusign Multipart Request To Create Envelope using java SDK (the current link for this source is here: https://raw.githubusercontent.com/docusign/code-examples-java/ee8489ca749a007767f07e1871ec9be100b5abb2/src/main/java/com/docusign/controller/eSignature/services/SendBinaryDocsService.java) or raw HTTP request as at Docusign - send envelope REST API (POST multipart/form-data ). The sample code uses the raw HttpsURLConnection class to make HTTP requests, different from how ApiClient uses Jersey to make requests.
Why is this common use case not just built into the SDK? The SDK does actually have some multipart support in ApiClient.java https://github.com/docusign/docusign-esign-java-client/blob/master/src/main/java/com/docusign/esign/client/ApiClient.java serialize() and invokeAPI() methods, using Jersey parts. Note that FormDataContentDisposition is used, which will result in individual parts having the name "form-data". Also this route is not available for the EnvelopesApi.createEnvelope() method.
It's tempting to just make a direct ApiClient.invokeAPI() call to try and make use of this, but the code starting "// Generate and add Content-Disposition header as per RFC 6266" seems like it overwrites the "entity" with each individual part, so this may not work with more than one part?
The documentation of the API is found here: https://developers.docusign.com/docs/esign-rest-api/how-to/send-binary/ Both the description and sample code use "Content-Disposition: file..." while the web spec states that for each individual part of a multipart body "The first directive is always form-data" (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) - which ApiClient itself does for its own multipart support.
ETA: experimentation shows that using "Content-Disposition: form-data" actually works fine
Clients are also asked to include "documentid=N" in the Content-Disposition, but it appears difficult to add this extra parameter at least when using Jersey's FormDataContentDisposition. If you look inside the source of glassfish FormDataContentDisposition / ContentDisposition you can see that only a fixed list of parameters will be included in the string buffer.
It would greatly help SDK users if we could get:
official support for creating envelopes with multiple binary documents in the official SDKs
alternatives to proprietary customizations of Content-Disposition header
this would make life much easier for Java SDK consumers. Apologies if I got anything wrong here.
A well written question. To better register your sdk enhancement request, see feedback.docusign.com and request via your DocuSign sales person.
THE SDKs are autogenerated from our swagger file. So your request would be a large project and is not currently planned.
While uploading with base64 encoding is inefficient, it works fine.

How to get specific information from a website using JAVA HTTP request

I want to write a program in java to make http request and print for me only the information I Want. For example there's this website: "https://br.investing.com/currencies/usd-brl"
And here I need to get only the information about the dolar value in real time. How do I do that in java? I thought it would be more easy using an API but I have no idea on how to do it.
I have no idea where to start or resources i should be using, I need to use only native java resources no third party modules.
there isn't API at 'investing.com' but simple widget. you should crawl the data OR using API via another site.
i think the Exchange rate information is provided by not only investing.com but also several finance service. And you can using Class URL, HttpUrlConnection in java.net to request for data to target url you need.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URL.html

JavaFX2 client-server via REST Json

Which is the way to create a JavaFX client-server app using Json packets for the communication? I should interact in real time between two different remote JavaFX instances (like in a chat) sending from one side to the other messages Json formatted.
Any example for doing that?
Thanks!
If you need a high level overview of how this could be done, then you need a library for http (You want to send JSON over http, right?) and some library to work with JSON. I think JavaFX2 is not the case in this problem, you can do this in any Java app.
There is a nice thing called Apache HttpComponents to work with http. Tutorials on their site should provide you with some tips of how to implement both server and client functionality.
To work with JSON you can use, for example, Google's GSON library. Some discussion of JSON libraries can be found here.
At first look you need to form a Java object, representing JSON, convert in to String and pack it up into an http POST request.
Hope this will be some help.

Using the REST API of Twilio

Not being familiar with REST and after reading some doc about it I am a little bit confused about the way it works.
I actually want to use Twilio SMS Gateway that provides a REST API to interact with and send text messages from an existing web-application.
From what I understand, REST is a way to structure a web service and in the end, instead of using SOAP for example, we just access 'resources' with URLs, relying on HTTP to GET, PUT or DELETE data.
The SMS Gateway I am talking about is providing a Java API that I could integrate to my web-app. The classes in this API uses httpcore, httpclient and commons-codec jars. Is this because REST rely on HTTP?
So basically, their API is relying on the Apache and HTTP libs to construct HTTP requests in Java and setting the basics, so I just have to provide with the data I want to submit and/or specific information?
REST API's are HTTP API's. The word REST is supposed to indicate something about how your API works. Basically that you use POST requests to update data and GET requests to retrieve it, and you have different HTTP endpoints for all of the different resources in your API, like Calls or Recordings.
The Twilio helper libraries (including the Java library) are basically wrappers around HTTP calls to the Twilio API. The idea was to make it easier for you to make API calls to Twilio by abstracting away the HTTP authentication and request stuff behind some more language-specific code. We also parse the HTTP response into an object for you.

How can I implement Hunch's API in java?

I'm creating a Java application that uses Hunch's API, but the problem is I have no clue as to how I can use a web API in Java. Any ideas?
That depends on what the API provides, generally you'd need a web server (like Tomcat) to run a web framework.
If it's just an api to communicate with a service via HTTP (maybe webservices) then have a look at the corresponding libraries (JAX-WS etc.).
Edit: I looked up Hunch API, and assume it is this: http://hunch.com/developers/v1/
It seems like it would return JSON encoded data, so you might use the URL class to read data from an URL. That data would be a JSON string which then needs to be decoded using one of the many JSON libraries out there (e.g. JSON-simple).

Categories