How do I determine the audio format of a online stream? - java

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.

Related

Get the raw http request/response message in java

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

Groovy Scripts to Stored image/png content type as .png in SOAP UI

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.

Trying to download html page via url in JAVA. Getting some weird symbols instead

So I am trying to download this page http://www.csfd.cz/film/895-28-dni-pote/prehled/.
I am using this code:
URL url = new URL("http://www.csfd.cz/film/895-28-dni-pote/prehled/");
try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),Charset.forName("UTF-8")))){
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
It worked on some other pages, but now it is giving me some weird symbols. For example the second line I get is: "�\�?�����c��n��". (It has not been copied exactly as I see it in eclipse console.)
I think I am using UTF-8 encoding as is the page.
In case you are wondering it is in Czech.
Thanks for help.
$ curl -D- http://www.csfd.cz/film/895-28-dni-pote/prehled/
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 01 Feb 2016 08:11:36 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
X-Frame-Options: SAMEORIGIN
X-Powered-By: Nette Framework
Vary: X-Requested-With
X-From-Cache: TRUE
Content-Encoding: gzip`
▒}I▒▒▒▒^▒▒29B▒▒▒$R▒M▒$nER▒▒4X, #
etc....
Notice Content-Encoding: gzip - the content is compressed using gzip, and you will need to decompress it in order to use it.
Study the classes in java.util.zip, especially GzipInputStream, which I believe you can wrap around a regular input stream.

Testing of Raw HTTP post in java, 400 Bad Request

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.

HttpURLConnection getContentType not working for SSL connection

For a specific HTTPS URL, the following java code does not correctly retrieve the Content-Type:
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
String mimeType = connection.getContentType();
if (mimeType == null) throw new Exception("Could not detemine mimetype for remote URL.");
However if I curl the URL with -I, I get the following:
HTTP/1.1 200 OK
Date: Wed, 23 Jul 2014 00:50:01 GMT
Server: Apache/2.4.7 (Ubuntu)
Cache-Control: max-age=0, no-cache
Connection: close
Content-Type: text/html
Additionally, if I run the Java code without the URL being HTTPS, getContentType does retrieve the mime type successfully. Finally, the Java code does work for other servers that are connected to with HTTPS.
The URL was using AES-256 SSL and I was running into this: Java SSL DH Keypair Generation - Prime Size Error
I'll either have to update my java or use bouncy castle.

Categories