How to refresh website until server has response ? in Java? - java

I try to entry a busy website but always get the busy server result(error 500).
And I would like to use java code to loop the website until I can entry the website but i only know how to get the response code don't know how to open it in browse.
How to open the website in the connection of HttpURLConnection in browse or is there any other way to open or loop the website until no error? Many thanks!!!
import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;
public class test{
public static String URL = "http://www.example.com/";
public static void main (String args[]) throws Exception{
int i =0;
while(i==0){
URL url = new URL("http://www.example.com/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
if (code != 500){
//how to open the no error website.
}
System.out.println(code);
}
}
}

If you would like to open a website upon receiving the 200 HTTP Status, you can do something like below inside your if ... else statement :
if (code == 200){
//how to open the no error website.
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(url.toURI()); //referring to the url connection already created
System.exit(0);
}
}

Related

Why OutputStreamWriter doesn't write data?

I'm very new to android programming and I have a problem with OutputStreamWriter. Now i'm working on simple test appication to send a POST requests to a server. There is a PHP application on the server side, which is designed to write data in a txt file.
When i run my android app it goes through the code and the commands in the "finally" block are performed. The server really writes in the txt file, but it puts empty data.
I've tried also with GET method and tried to check REQUEST array. But no data are sent.
If i delete from my code OutputStreamWriter I obtain the same picture, so it doesn't work at all.
I also tried with using encoding, the same result.
I have done through similar questions:
OutputStreamWriter not writing -
writing to file and problem was with its creating;
Android, Java: HTTP POST Request
- here and a few other questions is used httpClient which is depricated now.
HttpUrlConnection getOutputStream have plroblem, HttpUrlConnection getOutputStream have plroblem - here networking at main thread, but mine is run on the another one;
Android POST request not working, Java HttpURLConnection OutputStreamWriter is empty - is proposed to use reader to receive input stream. I've tried this solution, but the result was the same (no data sent)
My android version 6.0.1.
What is the problem may be in?
package com.victoria.requester;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.*;
import java.net.*;
import android.widget.TextView;
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread.start();
}
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
String url = "http://192.168.56.226:90/wfile.php";
String f_name = "123";
HttpURLConnection httpCon = null;
TextView textView = findViewById(R.id.textView);
textView.setText("OK1!");
try {
textView.setText("OK2!");
URL urlObj = new URL(url);
httpCon = (HttpURLConnection) urlObj.openConnection();
httpCon.setDoInput(true);
httpCon.setDoOutput(true);
//httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpCon.setRequestProperty("Content-Type", "text/html");
httpCon.setRequestMethod("POST");
String parameters = "f_name"+f_name;
//String parameters = URLEncoder.encode("f_name", "UTF-8") + "=" + URLEncoder.encode("123", "UTF-8");
httpCon.getResponseCode();
OutputStreamWriter writer = new OutputStreamWriter(httpCon.getOutputStream());
writer.write(parameters);
writer.close();
} catch (MalformedURLException e) {
textView.setText("bad URL!");
} catch (IOException e) {
textView.setText("network error!");
} finally {
httpCon.disconnect();
textView.setText("All done");
}
}
});
}
So, I've finally solved my problem, which actually was complex.
First of all, thank you, #Stephen C.
When I moved getResponseCode AFTER getOutputStream() my server begun to receive requests.
It's important that without calling getResponseCode the data weren't sent.
But the second trouble was with string:
String parameters = "f_name" + f_name;
As I lost "=" the name of the element in the POST array was 'f_name123' and value was empty))) That's why i couldn't pick the data up from $_POST["f_name"].
Also I'd like to notice that textview doesn't work only if accessing within the connection. After disconnected you can use textview.
Thank to all for help.
The next code works for me now:
package com.victoria.requester;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.*;
import java.net.*;
import android.widget.TextView;
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread.start();
}
Thread thread;
{
thread = new Thread(new Runnable() {
#Override
public void run() {
String url = "http://192.168.56.226:90/wfile.php";
String f_name = "123";
HttpURLConnection httpCon = null;
TextView textView = findViewById(R.id.textView);
try {
URL urlObj = new URL(url);
httpCon = (HttpURLConnection) urlObj.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpCon.setRequestMethod("POST");
String parameters = "f_name=" + f_name;
OutputStreamWriter writer = new
OutputStreamWriter(httpCon.getOutputStream());
writer.write(parameters);
writer.flush();
writer.close();
httpCon.getResponseCode();
} catch (MalformedURLException e) {
textView.setText("bad URL!");
} catch (IOException e) {
textView.setText("network error!");
} finally {
httpCon.disconnect();
textView.setText("All done");
}
}
});
}
}
The problem is that you are calling httpCon.getOutputStream() and writing the request data after you have called httpCon.getResponseCode().
The correct order is:
Call getConnection() to get the connection object for the URL
In any order:
Set the request method
Set any request properties
Call setDoOutput(true) since you intend to use the output stream.
Call getOutputStream()
Write the data.
Close the stream.
Get the response code.
What you are currently doing will throw an exception when you call getOutputStream. The remote server can only send you a response after it has read the request data. When you call getResponseCode() this tells the client-side stack to indicate to the server that there is no more request data.
In addition, you should not call setDoInput(true) if you don't intend to read data from the connection in some circumstances. At the very least it is redundant.

Downloaded File Has 0 Bytes

I'm trying to download a .mp3 music file from this URL to the project root directory but the downloaded file always has 0 bytes in size (it is blank). The download also immediately stops.
I'm using the following code:
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class MusicDownloader
{
public static void main(String[] arguments) throws MalformedURLException, IOException
{
download("https://www.youtube.com/audiolibrary_download?vid=d0a68933f592c297", "Ponies and Balloons");
}
public static void download(String url, String fileName) throws MalformedURLException, IOException
{
FileUtils.copyURLToFile(new URL(url), new File(fileName + ".mp3"));
}
}
In a browser, downloading the file manually works flawlessly. A download link from another website e.g. this one had no problems to be processed by the code. What could be the problem here?
Sending a valid user-agent String doesn't work either.
The problem is actually with your URL https://www.youtube.com/audiolibrary_download?vid=d0a68933f592c297. It is actually issuing a redirect as Resource Temp Moved - 301 status code. So you need to pick its new URL. I tried using it HttpURLConnection to see that new redirected url is https://youtube-audio-library.storage.googleapis.com/d0a68933f592c297. You can use the below code :-
String urlString = "https://www.youtube.com/audiolibrary_download?vid=d0a68933f592c297";
URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
int statusCode = huc.getResponseCode(); //get response code
if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP
|| statusCode == HttpURLConnection.HTTP_MOVED_PERM){ // if file is moved, then pick new URL
urlString = huc.getHeaderField("Location");
url = new URL(urlString);
huc = (HttpURLConnection)url.openConnection();
}
System.out.println(urlString);
InputStream is = huc.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream("test.mp3");
int i = 0;
while ((i = bis.read()) != -1)
fos.write(i);
The same effect you can check is available in FileUtils or not. I am sure, it should be . Cheers :)
Because it is illegal and against Youtube Terms of Service
Youtube specifically blocks most generic ways of downloading mp3 off their site. A simple 10ish lines of code won't work or piracy would've been bigger than it already is.
If they catch you, you WILL be blocked

How to connect to TMDB api

Here is my api key: 7b5e30851a9285340e78c201c4e4ab99
And I am trying to connect to TMDB api: here is my code:
package movieDBapiconnnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class connection {
public static void main(String[] args) throws Exception{
URL url = new URL("http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
}
}
But it always showing me the error that:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at movieDBapiconnnection.connection.main(connection.java:17)
First, I would use a TMDB wrapper. Get a Java wrapper at: https://github.com/Omertron/api-themoviedb. Use the fully tested and tried wrapper rather than trying to connect to it and creating your models from scratch. Typically in a wrapper when you instantiate a class, you pass in the API key into the constructor and the wrapper does the rest.
Your error is simply because of extra characters in your API key parameter
http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99/3/movie/550
remove the /3/movie/550, as this is just a typo,
so the correct one is
http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99
Note:
Change 7b5e30851a9285340e78c201c4e4ab99 into your correct API key
You passed the wrong URL, pass this URL
"http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99
URL url = new URL("http://api.themoviedb.org/3/movie/550?api_key=7b5e30851a9285340e78c201c4e4ab99");
try to the the networking part in a background thread.
using main thread for this kind of operation will likely give you a similar error.
use an AsynchTask and doInBackground() to achieve this.

How do I get my Java code to display results after connection?

I have java code which connects to a PHP script I've written and posts to it. The PHP contacts an API for evaluation and returns the results in html format.
The Java appears to work, but in Eclipse the result is raw html, not rendered form.
I would like to get my results to launch in a browser. I tried placing it in my xampp folder, but that did nothing, it just downloaded the Java script upon clicking the file.
Any ideas on how I can accomplish this? I am open to changing the PHP code somehow to have it just return the variables and having Java create some form for the user to see. I'm just not so adept at Java right now. Ideas and examples are great!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Connect {
public void POSTDATA() {
}
public static void main(String[] args) {
try {
// Construct data
String data = URLEncoder.encode("ipaddress", "UTF-8") + "=" + URLEncoder.encode("98.36.2.53", "UTF-8");
// Send data
URL url = new URL("http://localhost/myfiles/WorkingVersion.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
}
}
}
Your Java application simply connects to the server and does a POST with ipaddress=98.36.2.53. If you want to display the result in a browser, why use Java anyway?
Several easy solutions are:
Rewrite your PHP script to accept the parameter via GET and access it via an URL from the Webbrowser http://localhost/myfiles/WorkingVersion.php?ipaddress=98.36.2.53
Write an HTML page that uses a form to POST the data - e.g. by having an <input type="hidden" name="ipaddress" value="98.36.2.53">. You will need user interaction to post the from, but maybe this is sufficient
Use JavaScript to access the server, do the POST request and read the data. As JavaScript runs in the browser, it is easy to display it on the webpage (e.g. by using jQuery's .html( htmlString ) method.

Why does this HTTP servlet behave inconsistently?

An intranet site has a search form which uses AJAX to call a servlet on a different domain for search suggestions.
This works in Internet Explorer with the intranet domain being a "trusted site" and with cross-domain requests enabled for trusted sites, but doesn't work in Firefox.
I have tried to work around the problem by creating a servlet on the intranet server, so there's a JS call to my servlet on the same domain, then my servlet calls the suggestions servlet on the other domain. The cross-domain call is server-side, so it should work regardless of browser settings.
The AJAX call and my servlet's call to the other servlet both use a HTTP POST request with arguments in the URL and empty request-content.
The reason I'm sticking with POST requests is that the JS code is all in files on the search server, which I can't modify, and that code uses POST requests.
I've tried calling the customer's existing suggestions servlet with a GET request, and it produces a 404 error.
The problem is that the result is inconsistent.
I've used System.out.println calls to show the full URL and size of the result on the server log.
The output first seemed to change depending on the calling browser and/or website, but now seems to change even between sessions of the same browser.
E.g. entering "g" in the search box, I got this output from the first few tries on the Development environment using Firefox:
Search suggestion URL: http://searchdev.companyname.com.au/suggest?q=g&max=10&site=All&client=ie&access=p&format=rich
Search suggestion result length: 64
Initial tries with Firefox on the Test environment (different intranet server but same search server) produced a result length of 0 for the same search URL.
Initial tries with Internet Explorer produced a result length of 0 in both environments.
Then I tried searching for different letters, and found that "t" produced a result in IE when "g" hadn't.
After closing the browsers and leaving it for a while, I tried again and got different results.
E.g. Using Firefox and trying "g" in the Development environment now produces no result when it was previously producing one.
The inconsistency makes me think something is wrong with my servlet code, which is shown below. What could be causing the problem?
I think the search suggestions are being provided by a Google Search Appliance, and the JS files on the search server all seem to have come from Google.
The actual AJAX call is this line in one file:
XH_XmlHttpPOST(xmlhttp, url, '', handler);
The XH_XmlHttpPOST function is as follows in another file:
function XH_XmlHttpPOST(xmlHttp, url, data, handler) {
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = handler;
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-Length",
/** #type {string} */ (data.length));
XH_XmlHttpSend(xmlHttp, data);
}
Here is my servlet code:
package com.companyname.theme;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class suggest extends HttpServlet {
Properties props=null;
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String result = "";
String args = req.getQueryString();
String baseURL = props.getProperty("searchFormBaseURL");
String urlStr = baseURL + "/suggest?" + args;
System.out.println("Search suggestion URL: " + urlStr);
try {
int avail, rCount;
int totalCount = 0;
byte[] ba = null;
byte[] bCopy;
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write("".getBytes());
os.close();
InputStream is = conn.getInputStream();
while ((avail = is.available()) > 0) {
if (ba == null) ba = new byte[avail];
else if (totalCount + avail > ba.length) {
// Resize ba if there's more data available.
bCopy = new byte[totalCount + avail];
System.arraycopy(ba, 0, bCopy, 0, totalCount);
ba = bCopy;
bCopy = null;
}
rCount = is.read(ba, totalCount, avail);
if (rCount < 0) break;
totalCount += rCount;
}
is.close();
conn.disconnect();
result = (ba == null ? "" : new String(ba));
System.out.println("Search suggestion result length: " + Integer.toString(result.length()));
} catch(MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
PrintWriter pw = resp.getWriter();
pw.print(result);
}
#Override
public void init() throws ServletException {
super.init();
InputStream stream = this.getClass().getResourceAsStream("/WEB-INF/lib/endeavour.properties");
props = new Properties();
try {
props.load(stream);
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
Solution: don't rely on InputStream.available().
The JavaDoc for that method says it always returns 0.
HttpURLConnection.getInputStream() actually returns a HttpInputStream, in which available() seems to work but apparently sometimes returns 0 when there is more data.
I changed my read loop to not use available() at all, and now it consistently returns the expected results.
The working servlet is below.
package com.integral.ie.theme;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class suggest extends HttpServlet implements
javax.servlet.Servlet {
Properties props=null;
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//super.doPost(req, resp);
final int maxRead=200;
String result="";
String args=req.getQueryString();
String baseURL=props.getProperty("searchFormBaseURL");
String urlStr=baseURL+"/suggest?"+args;
//System.out.println("Search suggestion URL: "+urlStr);
try {
int rCount=0;
int totalCount=0;
int baLen=maxRead;
byte[] ba=null;
byte[] bCopy;
URL url=new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
// Setting these properties may be unnecessary - just did it
// because the GSA javascript does it.
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length","0");
InputStream is=conn.getInputStream();
ba=new byte[baLen];
while (rCount>=0) {
try {
rCount=is.read(ba,totalCount,baLen-totalCount);
if (rCount>0) {
totalCount+=rCount;
if (totalCount>=baLen) {
baLen+=maxRead;
bCopy=new byte[baLen];
System.arraycopy(ba,0,bCopy,0,totalCount);
ba=bCopy;
bCopy=null;
}
}
} catch(IOException e) {
// IOException while reading - allow the method to return
// anything we've read so far.
}
}
is.close();
conn.disconnect();
result=(totalCount==0?"":new String(ba,0,totalCount));
//System.out.println("Search suggestion result length: "
//+Integer.toString(result.length()));
} catch(MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
PrintWriter pw=resp.getWriter();
pw.print(result);
}
#Override
public void init() throws ServletException {
super.init();
InputStream stream=this.getClass().getResourceAsStream("/WEB-INF/lib/endeavour.properties");
props=new Properties();
try {
props.load(stream);
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
Start with a unit test. Servlets are pretty straightforward to unit test and HttpUnit has worked for us.
Debugging Servlet code in a browser and with println calls will cost more time in the long run and it's difficult for someone on SO to digest all of that information to help you.
Also, consider using a JavaScript framework such as JQuery for your AJAX calls. In my opinion there's little reason to touch an xmlHttp object directly now that frameworks will hide that for you.

Categories

Resources