Solved

java new URL(). Exception when url is outside of local network.

Posted on 2008-10-28
15
612 Views
Last Modified: 2013-12-29
This method works fine if the url is within local network (from multiple computers) but gives
java.lang.ArrayIndexOutOfBoundsException: 1
if remote url is specified.

How can this be fixed?

Thank you.
try{

        

        // construct data

        String data = URLEncoder.encode("UserName", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8");

        data += "&" + URLEncoder.encode("Password", "UTF-8") + "=" + URLEncoder.encode(userPassword, "UTF-8");

        data += "&" + URLEncoder.encode("MAC", "UTF-8") + "=" + URLEncoder.encode(macAddress, "UTF-8");

        //data += "&" + URLEncoder.encode("browser", "UTF-8") + "=" + URLEncoder.encode(this.browserName, "UTF-8");

        

        // Send data

        //URL url = new URL("http://localhost/RecordInstallations.php");

        URL url = new URL("http://tmthv2.byethost13.com/RecordInstallations.php");

        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        

        conn.setUseCaches(false);

        conn.setInstanceFollowRedirects(false);

        conn.setAllowUserInteraction(false);

        conn.setDoOutput(true);

        conn.setDoInput(true);

        conn.setRequestMethod("POST");

        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        

        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) {

            processServerMessage(line);

        }

        wr.close();

        rd.close();

      } catch (Exception e){

          System.out.print(e);

      }

Open in new window

0
Comment
Question by:AlexanderR
  • 8
  • 7
15 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22824988
AlexanderR,

Hello again.

Try breaking it apart. http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html

URL url = new URL(String protocol, String host, int port, String file)

Also check the spelling of the hostname, should it be bytehost13?
try {

    URL url = new URL("http", "tmthv2.byethost13.com", 80, "RecordInstallations.php");

} catch (MalformedURLException e) {

    // ... handle error ...

}

Open in new window

0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22825114
Promissing suggestion but no go.

But i found something interesting.
URL url = new URL("http", "tmthv2.byethost13.com", 80, "RecordInstallations.php");
gives
java.io.IOException: Server returned HTTP response code: 400 for URL: http://tmthv2.byethost13.com:80RecordInstallations.php

So its communicating!

That url is obviously wrong so i changed it to
URL url = new URL("http", "tmthv2.byethost13.com", 80, "/RecordInstallations.php");
and got
java.lang.ArrayIndexOutOfBoundsException: 1

Does that mean anything?
0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22825203
Actually no, its not communicating.  Thats just error 400 because of invalid URL structure.  If i give it a non existing file name it gives me outofbounds thing.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22825398
Interesting?  Do you have a proxy?
0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22825428
No, just a usual linksys  router.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22826660
Hmmm.

I just tried from my machine and it worked (except I didn't have any data to post) so just posted nothing.  I was going to suggest using conn.connect() but I actually got it working with or without that.

Here is the response I got as I changed the processServerMessage line of code to:
System.out.println(line);
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>302 Found</title>

</head><body>

<h1>Found</h1>

<p>The document has moved <a href="http://byet.org/web/index.html">here</a>.</p>

</body></html>

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22826691
Through a debug on, as I just tested posting dummy data, so the message you are getting is either because of a connection problem out to internet or during the actual posting of real data and/or the processServerMessage routing.  Debug will tell you exactly where unless you are already get a line number in code from error message.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Author Comment

by:AlexanderR
ID: 22826839
Now, thats an interesting response...
So there is something wrong with the host then??
Perhaps they are somehow redirecting the response along the way.  I can go to the file i need through the browser but, as you noted, java gives me that html page and i have no clue where its comming from or why its there instead of my page.

I guess i will have to look into that.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22826945
I know on previous question you add these extra settings to get the postings to work in Opera (I think IIRC), but try to change this -- I got curious about trying a GET instead of POST as I got answer from web page in my browser too that says didn't recognize computer and to call you. :) Then looked at this line that thought was curious given the message was about redirecting page:

conn.setInstanceFollowRedirects(false);
change to true and get further in process.
0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22827028
After chaning it to true, it got a page with load of javascript on it which, i assume, the browser must process in order to go on to the page.  Obviously this java applet isn't going to be doing that.

Can try using text-based browser (lynx or links) to see whats going on

So this host is based on a bunch of complex redirects all over the place.  Thats a real problem...
Then there is nothing wrong with the Java Applet, and having it work with this host is a completly different question.  I guess i have to ask in their support forums if there is any way around it.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22827203
Sounds like it.  Sorry could be more of a help, but maybe that at least saves you losing sleep over code that is actually working fine.
0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22827224
Yes.  Your comments saved me lots of trouble.  Unlike in PHP, i am so new to Java that i still dont know where to begin looking yet.
I'll accept comment with conn.setInstanceFollowRedirects(true);
since it showed where the problem is.

Thanks a lot!
0
 
LVL 11

Author Closing Comment

by:AlexanderR
ID: 31510890
I would not be able to isolate the problem by myself.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22827238
No problem, you are welcome.

Btw, PHP 5+ (maybe lower too) can run Java classes directly, so if there are some functions you could do in PHP to this site, but some functionality you need in java like for a business object or the piece of code grabbing the MAC addresss, you can just create the java object to do its stuff but have other parts of your code in PHP.

Just some more food for thought.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22827244
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now