Java URL openStream throws exception - java

I have the following code, but when I run it I get an exception
"SocketTimeoutException" at openStream.
Code:
String urlStr = "https://www.nse-india.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=SCHNEIDER&series=EQ&fromDate=01-01-2020&toDate=29-02-2020&datePeriod=&hiddDwnld=true";
URL urlConn = new URL(urlStr);
InputStream in = urlConn.openStream();
When I execute the same URL from browser, it works fine.

The server looks for two request headers, the below code works
String urlStr = "https://www.nse-india.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=SCHNEIDER&series=EQ&fromDate=01-01-2020&toDate=29-02-2020&datePeriod=&hiddDwnld=true";
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
conn.setRequestProperty("accept-language", "en-US,en;q=0.9");
conn.setRequestProperty("user-agent", "MyJavaApp");
InputStream in = conn.getInputStream();

When I execute the same URL from browser, it works fine.
There is obviously a difference in what your browser does and what your JVM does. I guess that your browser has a HTTP proxy server configured, but your application hasn't?

Related

Java HttpUrlConnection is not connecting to 000webhostapp

URL url = new URL("http://subdomain.000webhostapp.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.getInputStream():
but the program gives ioexception for http error code 400. my browser too, it cant connect 000webhostapp

Use HttpsURLConnection to make HTTP and HTTPS request's

There is a way to use HttpsURLConnection to do HTTP and HTTPS request ?
I am using just a method to do API request's but in localhost I'm not using https, so I want to use a HttpsURLConnection to do https and http request's without check protocol to create HttpsURLConnection or HttpURLConnection
Thanks!
Sorry by my english
I don't understand the problem.
URL can be HTTP or HTTPS without change any piece of code:
URL cUrl = new URL("http://www.google.com");
//cUrl = new URL("https://www.google.com"); //<-- decomment this line to use an HTTPS
final URLConnection cURLConnection = cUrl.openConnection();
cURLConnection.connect();
if (cURLConnection instanceof HttpURLConnection) ...when url=http://...
else if (cURLConnection instanceof HttpsURLConnection) ...when url=https://...
else ...
HttpsURLConnection extends HttpURLConnection, so you can consider to have always an "HttpURLConnection" object except if/when you need to use specific HttpsURLConnection methods, than you can check the instance using "instanceof".
For now I solved with something like emandt suggest
URLConnection conn = url.openConnection();
HttpsURLConnection httpsConn = null;
HttpURLConnection httpConn = null;
boolean isOverHttps = conn instanceof HttpsURLConnection;
if (isOverHttps) {
httpsConn = (HttpsURLConnection) conn;
httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory());
}else{
httpConn = (HttpURLConnection) conn;
}
And to set params and get response
(isOverHttps ? httpsConn : httpConn).setRequestProperty("Content-Type", service.getConTypeService());
BufferedReader br = new BufferedReader(new InputStreamReader((isOverHttps ? httpsConn : httpConn).getInputStream(), "utf-8"));

Trying to submit a JSON body in an HTTP request gives java.io.FileNotFoundException

I am trying to test an API on an Android application. When I call an action on postman with a raw JSON body of "some string", I get a proper response. However, when I try to call the same action on Android, I get a java.io.FileNotFoundException
URL url = new URL("my_correct_url");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = urlConnection.getOutputStream();
os.write("some text".getBytes());
os.close();
// Following line gives: java.io.FileNotFoundException
InputStream in = urlConnection.getInputStream();
Could you help me spot the problem?
The action code I am calling is as follows:
// This is an ASP.NET Core action
[HttpPost]
[Route("test1")]
public string Test([FromBody] string s)
{
return "test output: " + s;
}
TIL, some text is not valid JSON, "some text" is. So changing the following line
os.write("some text".getBytes());
to
os.write("\"some text\"".getBytes());
solved my problem.

Download *.deb file with HttpURLConnection issue

I try to implement download function with HttpURLConnection and function work, but when the file suffix is ".deb" e.g. file1.deb, file2.deb, download the file is not complete.
why?
this my code
DownloadInfo downloadFile(String source, String saveDirectory)throws HTTPException {
URL url = new URL(source);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new HTTPException(responseCode);
}
String httpContent = getResponseHeadContent(connection);
Path saveFilePath = produceSavePath(source, saveDirectory);
Files.copy(connection.getInputStream(), saveFilePath, StandardCopyOption.REPLACE_EXISTING);
connection.disconnect();
DownloadInfo info = new DownloadInfo();
info.setFilePath(saveFilePath);
info.setHttpHeadContent(httpContent);
return info;
}
I got the reason because of link server is IIS. IIS does not serve the unknown file type, then ".deb" not in MIME type. I must manual to add it.

Getting HTTP audio and then playing it back in android

I have a API that takes a string and converts in into audio when I do a HTTP get in android. I want to be able to play it back when I recieve it but I don't know how to do this. Can someone help me. Here is my code so far:
public static String getHTML(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

Categories