I am starting a tomcat server in my local for a web application and it takes around 20 minutes to be up and running. I want to check if the web app is up and running and taking any requests via java. Any help?
My server is say at localhost:8001/myapp
Thanks in advance.
You can check it through many ways. Like... Set a servlet as start up on-load and inside it keep some loggers which files log messages along with exact time.
You can add something like localhost:8001/myapp/status to the app that would return information about current status. Then you can just sent http request from java and check the response
public String execute(String uri) throws Exception {
URL url = new URL(uri);
URLConnection connection = url.openConnection();
connection.setReadTimeout(1000);
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
StringBuffer outputLine = new StringBuffer();
while ((inputLine = in.readLine()) != null)
outputLine.append(inputLine);
in.close();
return outputLine.toString();
}
I guess I will call this method after a certain time period to see if I'm getting a timeout exception of the raw html.
Related
I'm trying to get HTML by URL in Java. But 301 Moved Permanently is all that I've got. Another URLs work. What's wrong? This is my code:
hh= new URL("http://hh.ru");
in = new BufferedReader(
new InputStreamReader(hh.openStream()));
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine).append("\n");
str=sb.toString();//returns 301
}
You're facing a redirect to other URL. It's quite normal and web site may have many reasons to redirect you. Just follow the redirect based on "Location" HTTP header like that:
URL hh= new URL("http://hh.ru");
URLConnection connection = hh.openConnection();
String redirect = connection.getHeaderField("Location");
if (redirect != null){
connection = new URL(redirect).openConnection();
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
System.out.println();
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
Your browser is following redirects automaticaly, but using URLConnection you should do it by your own. If it bothers you take a look at other Java HTTP client implementations, like Apache HTTP Client. Most of them are able to follow redirect automatically.
found this answer useful and improved a little due to the possibility of multiple redirections (e.g. 307 then 301).
URLConnection urlConnection = url.openConnection();
String redirect = urlConnection.getHeaderField("Location");
for (int i = 0; i < MAX_REDIRECTS ; i++) {
if (redirect != null) {
urlConnection = new URL(redirect).openConnection();
redirect = urlConnection.getHeaderField("Location");
} else {
break;
}
}
There's nothing wrong with your code. The message means that hh.ru is permanently moved to another domain.
I tested your code and it is ok, but when I use "hh.ru", the same problem as yours, and when I use lynx(command line browser) to connect to "hh.ru", it will show me that it is redirecting to another url and then show me that it is moved permanently and after that this alert:
"Alert!: This client does not contain support for HTTPS URLs"
Check if the URL provided is HTTP or HTTPS, consider adding protocol is you are using only domain name like http(s)://domainname.com/resource-name
Read: https://en.wikipedia.org/wiki/HTTP_301
I resolved mine when I put the specific file running on the server.
Instead of http://hh.ru,
I used http://hh.ru/index.php.
It worked for me
I am currently working on a chat project for school where two clients can send messages over a http server. I can already send POST requests with the chat message and the server saves it.
My problem now is that the client needs to know if new chat messages are available. I was trying to do it like this:
private void checkChat()
{
String url = "http://"+serverip+":"+serverport+"/requests";
while(verbunden==true)
{
try
{
URL requrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) requrl.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
gui.writeChat(response.toString());
}
catch(Exception ex)
{
}
}
}
but when the method is called the program doesnt work anymore because the server gets flooded I think.
So my question now is: how can I check for new chat messages from the server? I need to use httprequests for that but I dont know how.
First, you are sending this request in a loop :
while(verbunden==true)
But you didn't write any pause so as soon as a response is receive, the next request is send, this means probably 100 request/second (based on the response time).
Simply add a Thread.sleep(5000 /*5000ms*/); in the loop to add a small break.
PS: you could have used two Socket to communicate between the server and client, that way the server can inform the client for a new message.
I'm trying to use http get request on java, to my localhost on port number 4567.
I got the get request: "wget -q -O- http://localhost:4567/XXXX"
[XXXX is some parameter - not relevent].
I've found a java library java.net.URLConnection for these kind of things but it seems that the URLConnection object is supposed to receive the url/port/.. and all other kind of parameters (In other words, you have to construct the object yourself), however, I got the full http get request as I've written above. Is there a way to simply 'shoot' the request without dealing with constructing the field for URLConnection?
You can create the URL object using your URL, it will figure out ports and other things itself. Ref : https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://localhost:4567/XXXX");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Why don't you use Apache HTTPClient library. It is simple to use.
HttpClient client = new HttpClient();
refer the document http://hc.apache.org/httpclient-3.x/tutorial.html
I am trying to grab a site's source code using this code
private static String getUrlSource(String url) throws IOException {
URL url = new URL(url);
URLConnection urlConn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConn.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
When I do grab the site code this way I get an error about needing to allow cookies. Is there anyway to allow cookies in a java application just so I can grab some source code? I do have the cookie my browser uses to log me in if that helps.
Thanks
John
This way you would have to deal with raw request data, Go with apache http client that gives you abstraction and some methods to allow to set headers in request
I have created a web application, and hosted in on the server. Now I want to create a java program which will access (or "hit") the URL of my application in a loop, so that I can check how much load can my web application can handle. Also, the program should be able to tell me when the URL was successfully accessed, and when it wasn't.
I tried executing it without using a loop:
try {
URL url = new URL("https://example.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {
System.out.println("e: " + e.toString());
}
But, I got this error:
e: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching example.com found.
Use,
javax.net.ssl.HttpsURLConnection
to connect to https://
something like (note, handle resource closing, exceptions etc left on you)
final URL url = new URL("https://example.com");
final HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
final BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
while ((input = br.readLine()) != null){
System.out.println(input);
}
However there are lots of tool available to load test it
you are just getting a stream from url, but obviously this url is using HTTPS, so you need a "Public Key" imported to your client application. otherwise the client and server won't be able to communicate with each other.