Solved

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

Posted on 2008-10-28
15
623 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 40
thymeleaf natural templating vs JSP 2 95
even odd program using while loop 3 40
Chrome and Firefox Java 5 23
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

813 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

15 Experts available now in Live!

Get 1:1 Help Now