Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URL HttpURLConnection return no response

Posted on 2009-04-03
13
Medium Priority
?
274 Views
Last Modified: 2012-05-06
The following Java code works okay as a standalone program but when I run it in a web application it has issues.

The issue is the web content returned doesn't have a http response.  It just returns binary content.  The http response is necessary because it gets forward to a browser using a proxy.  The browser can't interpret the content which is necessary for the application to work.

Any ideas what could be the problem?
package urlgetbytes;
 
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
/**
 *
 */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
        // TODO code application logic here
        URL url = new URL("http://localwebsite");
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
 
        uc.setRequestProperty("Host", "localwebsite");
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1");
        uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        uc.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
        uc.setRequestProperty("Accept-Encoding", "gzip,deflate");
        uc.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        uc.setRequestProperty("Keep-Alive", "300");
        uc.setRequestProperty("Connection", "keep-alive");
 
        uc.connect();
        InputStream in = uc.getInputStream();
 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
 
        byte[] b = new byte[1024];
        int i = in.read(b);
        while (i != -1) {
            out.write(b, 0, i);
            i = in.read(b);
            System.out.println("i: " + i);
        }
        out.close();
        in.close();
 
        byte[] response = out.toByteArray();
    }
}

Open in new window

0
Comment
Question by:lcor
[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
  • 7
  • 6
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24065432
how are you running it in your webapp?

why aren't you using the url class?

http://helpdesk.objects.com.au/java/how-to-write-get-response-to-byte-array
0
 

Author Comment

by:lcor
ID: 24077089
I am using the URL class.

See first line:
URL url = new URL("http://localwebsite");
0
 

Author Comment

by:lcor
ID: 24079206
I did a test.  I created a standalone web application and placed this code in a servlet.  I'm finding I get no http response.

I created a test java application (not web) and placed the same exact code there.  I get an http response.

Why does the servlet affect this code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 24082509
what exactly do you mean by no response? have you checked the respomnse code?

are any proxy servers involved?

0
 

Author Comment

by:lcor
ID: 24087521
yes, there's a proxy server so that's why I have to get the content.   Here's the flow.  The browser is configured to use a proxy

above code -> java client -> proxy -> browser

I got the response using get Headers and placed the HTTP OK header in the byte array.  The result is the  content that finally gets to the browser is incorrect.  The browser then reports an compression error.

There must be something wrong with the above code.

0
 
LVL 92

Expert Comment

by:objects
ID: 24092776
whats the browser got to do with it?

0
 

Author Comment

by:lcor
ID: 24097165
Firefox
0
 

Author Comment

by:lcor
ID: 24098230
the client sends the content gotten from the above code to the browser.  

If you were run the above code, examine the content with wireshark, with actual content gotten by the browser alone, you will see they are not same.
0
 
LVL 92

Expert Comment

by:objects
ID: 24103036
how is it different?

0
 

Author Comment

by:lcor
ID: 24107061
The content returned by url class is much larger than the real line signature.
0
 
LVL 92

Expert Comment

by:objects
ID: 24113025
i need more details from you to help, did you try the code I posted in the link above?

0
 

Author Comment

by:lcor
ID: 24115409
Yes, I tried that..the problem for my case is that I need the exact "type" of content returned that the browser would get.  I can't just use the code you posted alone.  I have to set request properties.  I took the exact GET properties for a real website and set request properties.  I then added them to your code.  The results do not return the same content a browser returns.  In fact, the content was somewhat strange.  The url class tended to get more bytes than needed. I tried the url class and setting request properties with multiple websites.  I had to revert to the socket class in order to get the same type behavior a browser gets.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24120418
That won't work as the behaviour is typically dynamic
try HttpClient, it gives you greater control over what is sent.

0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
Suggested Courses

715 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