I want the raw http request and response when sending an http request using java.
is there any way to do it without writing it manually? maybe adding tee with InputStream in the middle to capture it? or other solution?
i am currently using java.net.http library:
HttpRequest httpsRequest = getHttpsRequest(url, parameters, method, headers);
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> httpResponse = client.send(httpsRequest,HttpResponse.BodyHandlers.ofString(UTF_8));
String rawHttpMessage = ?
raw http message for example:
HTTP/1.1 200 OK
Date: Sun, 10 Oct 2010 23:26:07 GMT
Server: Apache/2.2.8 (Ubuntu) mod_ssl/2.2.8 OpenSSL/0.9.8g
Last-Modified: Sun, 26 Sep 2010 22:04:35 GMT
ETag: "45b6-834-49130cc1182c0"
Accept-Ranges: bytes
Content-Length: 12
Connection: close
Content-Type: text/html
Hello world!
request and response http message
Related
I have an online audio stream at https://iceant.antfarm.co.za/EdenFM. You can simply paste the audio stream in your browser to hear the audio. My question is: "How do I determine the audio format of the stream? Can this be done with a Linux command?
You can do this with curl by fetching only the header with the -I or --head option. The audio format can be determined based on the Content-Type in the response.
curl -I https://iceant.antfarm.co.za/EdenFM
With grep you can filter the relevant line:
curl -I -s https://iceant.antfarm.co.za/EdenFM | grep -i "^Content-Type:"
Which outputs this:
Content-Type: audio/aac
Of course this can be done in Java as well by sending a HEAD request to the desired endpoint:
URL url = new URL("https://iceant.antfarm.co.za/EdenFM");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("HEAD");
con.connect();
String contentType = con.getContentType();
System.out.println(contentType);
// output: audio/aac
Here is a version with the new HttpClient in Java 11:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://iceant.antfarm.co.za/EdenFM"))
.method("HEAD", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<Void> response = client.send(request,
HttpResponse.BodyHandlers.discarding());
HttpHeaders headers = response.headers();
headers.firstValue("Content-Type").ifPresent(System.out::println);
// output: audio/aac
It might be possible that you need to overcome some additional hurdles with SSL/TLS certificates depending on the server, your Java version and other factors.
You tag your question with tag::Java but your question only mentions a shell command, so here you are:
$ curl -I https://iceant.antfarm.co.za/EdenFM
will get you:
HTTP/1.1 200 OK
Content-Type: audio/aac
Date: Thu, 08 Jul 2021 13:33:16 GMT
icy-description:EDEN FM – Your Voice in Paradise
icy-genre:Various
icy-metadata:1
icy-name:EdenFM
icy-pub:1
Server: Icecast 2.4.0-kh13
Cache-Control: no-cache, no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type
Access-Control-Allow-Methods: GET, OPTIONS, HEAD
Connection: Close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
The relevant line is of course:
Content-Type: audio/aac
That you can get with a grep.
I am hitting API which is returning me the response like this
<data contentType="image/png;charset=UTF-8" contentLength="57789">Rescre....</data>
I want to store the response as an image file (.png) using groovy script on soap UI.
I tried something like that :-
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
String dirName="C:\\";
def httpResponse = context.testCase.testSteps["GetPNG"].testRequest.response.getContentAsString();
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(httpResponse.getBytes(Charset.forName("UTF-8"))));
ImageIO.write(imag, "png", new File(dirName,"snap.jpg"));
I am getting response properly. when i tried to convert it into buffered image.It's coming as null .
Raw Response looks something like this
HTTP/1.1 200
Server: nginx
Date: Mon, 26 Sep 2016 03:00:57 GMT
Content-Type: image/png;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-RateLimit-Limit: 15
X-RateLimit-Remaining: 14
X-RateLimit-Reset: 0
‰PNG.....special characters
Any helps appreciated.
I am currently trying to develop a Java based app to access OneDrive.
Today i tried to implement the download as described here: https://dev.onedrive.com/items/download.htm
I wanted to use the range parameter, to offer the user the capability to pause large downloads. But no matter how i send the parameter be at within the HTTP-Request header or in the URL as a GET-Parameter it will always send me the complete file.
Things i tried so far:
https:/ /api.onedrive.com/v1.0/drive/items/***/content?range=0-8388607
(OAuth via HTTP header)
https:/ /api.onedrive.com/v1.0/drive/items/***/content:
Header: Authorization: ***
range: 0-8388607
https:/ /api.onedrive.com/v1.0/drive/items/***/content:
Header: Authorization: ***
range: bytes=0-8388607
I also tried Content-Range and various variations on lower and upper case without success. Any reason why this dose not work?
PS.:
The links a broken because i am using a new account that only allows 2 links per post, I am aware that ther is a space between the two // in my post ;)
Requesting the range of the file is supported. You might want to use fiddler or some other tool to see if the original headers are being passed after the 302 redirect is performed. Below are the HTTP requests and responses when I provide the range header which is being passed on after the 302 redirect occurs. You'll notice that a HTTP 206 partial content response is returned. Additionally, to resume a download, you can use "Range: bytes=1025-" or whatever the last byte received was. I hope that helps.
GET https://api.onedrive.com/v1.0/drive/items/item-id/content HTTP/1.1
Authorization: Bearer
Range: bytes=0-1024
Host: api.onedrive.com
HTTP/1.1 302 Found
Content-Length: 0
Location: https://kplnyq.dm2302.livefilestore.com/edited_location
Other headers removed
GET https://kplnyq.dm2302.livefilestore.com/edited_location
Range: bytes=0-1024
Host: kplnyq.dm2302.livefilestore.com
HTTP/1.1 206 Partial Content
Cache-Control: public
Content-Length: 1025
Content-Type: audio/mpeg
Content-Location: https://kplnyq.dm2302.livefilestore.com/edited_location
Content-Range: bytes 0-1024/4842585
Expires: Tue, 11 Aug 2015 21:34:52 GMT
Last-Modified: Mon, 12 Dec 2011 21:33:41 GMT
Accept-Ranges: bytes
Server: Microsoft-HTTPAPI/2.0
Other headers removed
I have developed a raw http post in java. I am trying to post a file to the post request dump website http://www.posttestserver.com/. But it shows and error
400 Bad Request. Pleas let me know what need to be done to avoid this error.
In this code , output => Stream to write on server.
filename -> path on server, here filename is initated to post.php
output.println("POST"+" "+filename+" HTTP/1.1\r");
//output.println("Content-Length: "+data.length());
output.println("Content-Type: multipart/form-data, boundary=AaB03x\r");
output.println("Content-length: 100\r");
//As http1.1 is by default keep-alive , close the connection explicitly
output.println("Connection: Close");
// blank line
output.println();
output.println("--AaB03x");
output.print(
"--AaB03x Content-Disposition: form-data; name=\"fileID\"; filename=\"temp.txt\" Content-Type: text/plain "
+"/nHello How are you?"
+ "/n--AaB03x--");
output.flush();
Error is
HTTP/1.1 400 Bad Request
Date: Wed, 18 Mar 2015 02:22:00 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1
400 Bad Request
Bad Request
Your browser sent a request that this server could not understand.
This might be the issue of Content type. Server is expecting a request having header of content type text/HTML but your request content type is multipart/form data.
I am currently working with Retrofit and Okhttp and I am trying to cache some GET responses.
My Code is:
OkHttpClient okHttpClient = new OkHttpClient();
File cacheDir = new File(System.getProperty("java.io.tmpdir"),
"ddcache");
HttpResponseCache cache = new HttpResponseCache(cacheDir, 2024);
okHttpClient.setResponseCache(cache);
OkClient cl=new OkClient(okHttpClient);
restAdapter = new RestAdapter.Builder().setEndpoint(API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(cl).build();
And the Log shows this header:
HTTP/1.1 200 OK
Cache-Control: max-age=7200
Connection: Keep-Alive
Content-Type: text/html
Date: Tue, 18 Mar 2014 18:38:16 GMT
Keep-Alive: timeout=3, max=100
OkHttp-Received-Millis: 1395167895452
OkHttp-Response-Source: NETWORK 200
OkHttp-Sent-Millis: 1395167895378
Server: Apache/2.2.26 (Unix)
Transfer-Encoding: chunked
X-Powered-By: PHP/5.3.28
I check the response by returning the Server Unix Time on every call and it always returns a new one which means the
Cache-Control: max-age=7200
gets totally ignored
The Journal File in the Cache gets also updated with "CLEAN" and "DIRTY" notes, but nothing gets cached.
Is there something obvious I do not see?
I think I had a similar problem. The size of the cache is set in kilobytes and you are setting it to only 2024 kilobytes. That not enough space for almost anything. Try setting it to "10L * 1024 * 1024" (10Mb) and see if that helps.