How do I send form and header data with Java? - java

I have a curl that will make a post request. I have tried it in the command line and it works fine, so I know all my values that I should use. However, I am quite lost when it comes to translating this into Java.
I need to send form data and header data. The header is my authorization, and the form data contains both Strings and Dates, but again, the problem is that I don't know how to translate this into Java.
I have tried using URIBuilder and adding my form data with the addParameter, but it seems as if that one can only accept Strings and does not have any function for setting header information?
So I tried using HttpPost instead where I found the addHeader function that enables me to add my header data, but I did not find any function for adding the form data with this option.
I would be thankful if anyone could help me in any way possible.

Related

What is HPACK Compression in APNs

thank you for reading my question. I have read numerous documents about HTTP/2 and tried to understand most important concepts for constructing a APNs server-side API in Java.
So far, I have a working code for creating Payload and initiating ssl handshake with APNs.
I am stuck with figuring out a way to send Push Notification to APNs, specifically sending a HTTP/2 POST request to the Apple server.
APNs requires the use of HPACK (header compression for HTTP/2), which
prevents repeated header keys and values. APNs maintains a small
dynamic table for HPACK. To help avoid filling up the APNs HPACK table
and necessitating the discarding of table data, encode headers in the
following way—especially when sending a large number of streams:
If I have understood correctly, I need to use Huffman Encoding for the header compression. If I am wrong on this, please correct me.
The :path value should be encoded as a literal header field without
indexing
The authorization request header, if present, should be encoded as a
literal header field without indexing
I read RFC 7541 for this, but have no idea what they are talking about. Please note that I am trying to understand the system and requirements to gain knowledge through this post, not just answers to the specific questions.
The appropriate encoding to employ for the apns-id, apns-expiration,
and apns-collapse-id request headers differs depending on whether it
is part of the initial or a subsequent POST operation, as follows:
The first time you send these headers, encode them with incremental
indexing to allow the header names to be added to the dynamic table
Subsequent times you send these headers, encode them as literal header
fields without indexing
What do they mean when they say, "encode them with incremental indexing to allow the header names to be added to the dynamic table" and "Subsequent times you send these headers, encode them as literal header fields without indexing" afterwards. I guess understanding one of two literal header fields with/out indexing will help me comprehend this better.
Thank you again for reading the question and please help me with this!!
I think you will be better off using a Java library that does HTTP/2 for you.
The Jetty HttpClient (disclaimer, I'm the maintainer) does exactly that, see here.
If instead you want to implement HPACK because you want to have fun, then you have to take your time and read carefully RFC 7541.
As an starting point for implementing it, you can read the many Java implementations of HPACK out there, from Jetty's to Netty's, Undertow's, etc.
All the questions you are asking (e.g. "what is a 'literal header field without indexing') are detailed in the RFC in their own sections.
Very coarsely, HPACK defines a mapping table that maps numbers to strings.
Both peer maintain this table in sync, so that the two tables always contain the same data (at rest).
When one peer sends a HPACK block, it sends the numbers, so that the receiving peer can use the numbers to access the HPACK table to obtain the strings.
For new/custom headers (think cookies), the sending peer sends the number and the string, so that the receiving peer can update its HPACK table. For the first time there is no compression, but the second time that the same header is sent, the sending peer just sends the number, since it knows that the other peer will have the string mapped already, and this gives good compression of HTTP headers.

Request.getParameter java to vb.net

I have a question about retrieving data from a client who is using java and i am using vb.net.
I am expecting a form posted to me and read the data.
My issues is when i do Request.Form("DATA") i get nothing from the client.
Now if i create a html form and post it to my url with the field "DATA" i can read everything fine. I can also loop through my form and see the field and the button if i right them out to the screen or to a text file. Code is below
response.write(Request.Form("DATA"))
OR
Dim entryName As String
For Each entryName In Request.Form
response.write("Entity Name: " & entryName)
Next
Either method above works fine for me but not for the client. When the client hits my page i see nothing at all no buttons no fields, nothing.
I am concerned he is not posting properly to me. I spoke with the developer and he said he would retrieve the data on his end by doing something like "Request.getparameter"
I do not know java at all but from what i read it sounds like "Request.getparameter" will grab any field out of a form or querysting that has the name specified aka my "DATA" field that i am looking for.
Can anyone explain to me what request.getparameter means in java and what the equivalent code would be in vb.net?
Again i do not know java at all and have searched on this for a while but cant quite find a definitive answer.
Thanks in advance.
It is correct that in Java, request.getParameter("DATA") will look in both the query string and posted form data, while in .NET, Request.Form("DATA") only looks at posted form data. Therefore, it seems likely that your client is sending the data in the query string, since you are not seeing it.
You have a few options. You could use Request.QueryString("DATA") to check only the query string, or either Request.Item("DATA") / Request("DATA") or Request.Params("DATA") to check both the query string and posted form data, plus cookies and server variables. I think Items and Params may be a little different in what they return, e.g. for multiple values. They are probably the closest equivalent to the Java request.getParameter function.

url-encode body of http request

Can someone explain to me what is actually the need to Url-encode
data sent in the body part of http requests when using
content-type: application/x-www-form-urlencoded
thanks
By "the need", do you mean "the purpose"?
If you're after the purple - it is simply there to tell the server what to expect: URL-encoded key=value pairs. It also allows the server to know what is not coming its way - the likes of multipart/form-data!
This allows the server to unambiguously know how to read incoming data.
The data is sent as one header (this is also why it has a size limit). As such, you definitely want to avoid stuff like: newlines, colons. In addition to this, you definitely want to escape = in data, so that it doesn't mess with the key=value structure. You also want to escape & for the same reason. URL-encoding does all that - so it only makes sense that whoever designed the HTTP protocol went for it!
There are multiple ways to send data to the server in a POST request; URL Encoded data is just one of several possible formats.
The client and server have to agree on the format of data in the POST body. URL Form encoded data is the easiest to use because of its universal support. Browsers support it natively. Every programming language allows you to read url encoded post parameters using a familiar syntax.
But of course, there is no need to use url form encoded. You can as easily send json or xml encoded POST body. As long as the client and server are in sync, you can even create a totally different encoding and use it.

The combination of HTTP POST and GET requests + Javascript calling in JAVA?

Okay, this is going to be hard to explain but here goes nothing:
Lately I've been working a lot with POST and GET requests, but now I want to send a POST/GET request to this site called: http://www.mangareader.net/
The main problem I'm facing is that I want to use the search function of this site. Normally I would send a get request or something like that, but apparently this search function doesn't work that way, it works with some kind of Javascript code? I don't know exactly what it is, but try typing "Elf" in the search bar, and you'll get a drop down list of all the mangas (Japanese comics) with the word "Elf" in them. I want to know how this process is called, and how I can implement it into a Java program. For instance:
Login into a website
- > Send an HTTP post request. Get HTML data back. Process the HTML data. Get the information I need from the HTML source.
Using a search function on a regular site like google.com or bing.com
- > Send get request. Get HTML data back. Process the HTML data. Get the information I need from the HTML source.
Using search function on mangareader.net
- > ??????????
How would I achieve this? A theoretic explanation is enough, but a practical example would be great as well.
If you analyse the javascript that runs when search you get the following:
GET http://www.mangareader.net/actions/search/?q=test&limit=100 [HTTP/1.1 200 OK 113ms]
In other words, you can search on the site by a GET-request to
http://www.mangareader.net/actions/search/?q=test&limit=100
Where ?q contains your search word.
This site uses an ajax call to get a | ( pipe symbol ) seperated list from the page
/actions/search?q=term
It parses this list using string split and then makes it into combobox.
I have little experience with java, but a simple GET request to this page should work
replace {term} with your search function.
http://www.mangareader.net/actions/search/?q={term}&limit=100
You can use chrome network monitor to see if for your self

getting JSON response from webpage

I don't know a lot about this area so please excuse me if my question is vague or stupid.
I have a webpage which uses javascript and AJAX to display live data. Every few seconds, a request is made and a JSON response is returned and the data on the webpage is updated.
What I want to do is create a program in Java that will basically capture every response and interpret the data. I have found libraries which handle the JSON format already. However, I don't know how to get the response using Java.
So for example, a live news feed. I would like to log the data as it appears.
Thanks
Basically what you need to do is make an HTTP GET request to the page that hosts the JSON. You can do this by using a Java HTTP client. The one in the link is from Apache Commons but I believe there is actually one built into Java that is relatively straight-forward to use. When you make a request, it will return a result object that you can then use to access the response data and information such as response headers, etc.

Categories