How to download a certain byte of the file via http - java

How to download first 125 bytes and 125 last byte file via HTTP protocol ?

I believe you want to send an appropriate Range header. See the HTTP/1.1 spec for more information. Be aware that not all servers will support this, mind you. You may need to transfer the whole file, just to get to the last 125 bytes. Of course, you can get just the first 125 bytes by requesing the whole thing, and then only reading the first 125 bytes before killing the connection.
In theory I believe you should be able to use:
Range: 0-124,-125
Note that this will give interesting results if the full response would be less than 250 bytes...
Accept-Ranges: bytes
Range: bytes=-255

send content-range in request headers for specific range of content as response. but for which the webserver has to support range requests

Related

Smart Card 61 xx Error - what exactly does this mean?

I've installed an applet I've written onto a smartcard - but when sending commands to it, keep getting the 61 xx response. This doesn't happen when I run it in JCIDE, only when on an actual card.
I understand that a response of 61 xx means 'response bytes remaining' and that the xx is how many bytes of data are left - but I'm not sure why this happens. Does the Le of the command APDU need to specify the number of bytes in response? Do I need do get the response somehow (and how do I do this)? or is there another reason I am getting this error?
I have written other applets using the same code (see below) and not had this issue
apdu.SetOutgoing();
apdu.setOutGoingLength(length);
apdu.sendByteslOng(buff, offset, length);
61xx is not an error, but a warning, see ISO 7816, part 4, section 5.6. Warnings are less severe and still keep their response data (which are discarded for errors).
There 61xx is one of the easiest status codes (beyond 90 00), because the reaction is simple and clear: send a Get Response command with P3/LE set to the number indicated in SW2 (the xx part of 61xx). The main reason for the status is, that T=0 protocol is unable to send arbitrary many bytes (so it just sends 61xx) or more than 256 bytes in a response (in which case it sends the first chunk and an indication of how many bytes follow-up; if more than 255, only 255 can be shown and repeated Get Response commands are required getting the remaining chunks).

How can I get the number of actual bytes transferred with OkHttp3?

OkHttp does a great job of transparently handling GZIP content encoding. When I call response.body().contentLength() I get the decoded size of the response.
How can I get the number of bytes actually transferred in the HTTP response?
Alternately, getting the value from the original Content-Length header would do.
I am trying to keep track of how many bytes I have downloaded over a metered connection.
Look at EventListener, which tracks bytes transmitted over the network.
https://square.github.io/okhttp/events/

Jetty: Way to get request and response byte size current request processing?

In a Servlet context using Jetty, I would like to know the number of bytes a request was, and the number of bytes the response was (not only the content) - this so that I can log and do stats on this in a Filter upon exiting out.
So far, I've found this:
For response content, I've found that the HttpServletResponse object is a HttpOutput, on which there is a getWritten() returning the number of bytes written - and also, there is a getHttpChannel() returning a HttpChannel, which again has getBytesWritten(). However, both of these only return the size of the content, evidently not including headers - easily seen by a 302 redirect having size 0.
I have also found that from HttpChannel, you can invoke getHttpTransport(), which is a HttpConnection. This has nice "bytesIn" and "bytesOut" LongAdders, which evidently do include all bytes - however, this is for the Connection, and thus with keep-alive, this includes the bytes for all request/responses that this Connection has performed, thus increasing for each request/response cycle that Connection is a part of. (Also, on HttpChannel, there is a getRequests(), which returns the number of requests served with this instance, some kind of average could seemingly be obtained).
Thus: Is there a way to get the total request and response byte sizes for the current request? Bonus for size of content of request too. (I realize that there are two "sizes" to take into account: The one over the wire, which can be compressed, and the actual uncompressed size).

URL Size in Google App Engine

I am calling a servlet in my app hosted on GAE. The issue I am having is that my request url is greater than 2048 characters and I am getting 400 Bad Request error. While here it is mentioned that we can make a request with 10MB of data. So how can we send a request with 10MB of data? I am currently using free quota. A similar question was asked long ago but it is not answered yet.
AppEngine limits aside, it doesn't make much sense to put 10MB of data in an URL.
When you take a look at the HTTP protocol, a GET-request looks like this
GET /path/to/resource?possibleParam=value HTTP/1.1
Host: www.example.com
a POST-request like this
POST /path/to/resource?possibleParam=value HTTP/1.1
Host: www.example.com
Content-Type: */*; charset=utf-8
Content-Length: 4242
here come the actual data with a length of 4242 bytes
So if you allow large amounts of data the in the URI of a GET request that would mean that the server doesn't know how much memory it has to allocate in order to receive the whole uri. So to get better performance it does come quite natural that one would restrict the length of GET requests and force you to use POST request instead where the Content-Length must be made known before actually sending bulks of information.
Let's take a look at the comments from other Stackoverflow users
tx802 said:
POST your data?
Alex Martelli, refering to the maximum allowed URL length, said:
it will never be extended to 10 MB -- that obviously calls for a POST
or PUT (where data goes in the body, not the URL!)
That should make sense now, because protocol-wise it doesn't make much sense to push megabytes of data as a URI.
Sending megabytes of data in the request would rather warrant POST or PUT as the request method. This way you can send a request totaling up to 10 megabytes as you've noticed on the referenced article.
The reason you're getting the 400 error is outlined in the urlfetch errors module API documentation; the maximum URL length allowed is 2048 characters.
There is currently an existing feature request for increasing this length; although it's unlikely that this will change in the near future. You can 'star' the issue to get further updates and/or provide your use case in the comments.

.Net client vs Java Server with raw data

I am writting a .Net/C# client to a Java Server on Solaris.
The Java server is writting Raw byte data in a Gziped format which I need to extract, but I am having trouble to read the data in the right buffer sizes. I read the message not-deterministicly incomplete or complete and can not read the second message in any case.
I am reading the bytes using the NetworkStream class with the DataAvailable property.
My guess is that it could be related to a little/big endian problem.
Do I need to use a special conversion to change the data from big into little Endian? Do I need to read the necessary bytes using the gzip header?
I used to use the same server with an uncompressed protocol before and had no problem using a StreamReader with the ReadLine function before, but that protocol was purely text based.
Edit: Unfortunately I have no choice as the remote server and protocol is given. Is the endiness part of the GZip format or do I only need to convert the header accordingly? The uncompressed data are pure UTF8-encoded strings with line breaks as delimiters.
The GZIP format is not complex. It is available in all its glory in a simple, accessible specification document, IETF RFC 1952.
The GZIP format specifies the bit-order for bytes. It is not tunable with a flag for endianness. The producer of a GZIP stream is responsible for conforming to the spec in that regard, and a consumer of a GZIP stream, likewise.
If I were debugging this, I would look at the bytes on either end of the wire and verify that the bytes going in are the same as the bytes coming out. That's enough to put aside the endian issues.
If you don't have success transmitting a GZIP bytestream, try transmitting test data - 16 bytes of 0xFF, followed by 16 bytes of 0xAA, etc etc. And then, verify that this is the data coming out the other end.
I'm sorry, I don't know what you mean by I read the message not-deterministicly incomplete or complete and can not read the second message in any case. Second message? What second message? The endianness shouldn't affect the amount of data you receive.
It feels to me that you don't have confidence that you are successfully transmitting data. I would suggest that you verify that before working on endian issues and GZIP format issues.

Categories

Resources