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
Related
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"));
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?
So far, I have this snippet.
URLConnection connection = null;
try {
connection = (new URL("some_link")).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
} catch (IOException e) {
}
The possible response codes are 200 and 404 and it's working fine when the response code is 200 (OK). My question is how can I find the response code received by my connection, for example: if the response code is 404, throw an exception and do smth there.
use something like this:
HttpURLConnection connection = (HttpURLConnection)new URL("URL_STRING")
.openConnection();
int statusCode = connection.getResponseCode();
Make it to a type of HTTPUrlConnection. Then you can use .getResponseCode();
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");
Setting properties of a connection do not carry forward to redirected connections
HttpURLConnection mConnection = (HttpURLConnection) url.openConnection();
mConnection = addRequestProperty("User-Agent", "Mozilla");
InputStream stream = mConnection.getInputStream();
if there is a 302 code, mConnection is redirected, but the user-agent is "Java/1.5.0_28".
Any suggestion how to handle this?
It didn't change, it started out that way.
addRequestProperty() won't override the default. Use setRequestProperty() instead.
HttpURLConnection mConnection = (HttpURLConnection) url.openConnection();
mConnection.setRequestProperty("User-Agent", "Mozilla");