?
Solved

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

Posted on 2008-10-28
15
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 60

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 60

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 60

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 60

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
 
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 60

Accepted Solution

by:
Kevin Cross earned 2000 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 60

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 60

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 60

Expert Comment

by:Kevin Cross
ID: 22827244
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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