• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Posting to URLs with NON CGI Applications

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to post to URLs using the Std OutputStream and InputStream from the HttpUrlConnection.The sample code is below.
The code works fine with sites hosting CGI Applications - The Response is recd.
But for sites hosting Servlets/Jsp/Asp the response is Not recd.
Am I missing something ? Or does it never works for Servlets ?
Appreciate if someone could shed some light.
(The URL and the Query to be sent in the code would obviously be diferent.)
------------------------------------------------------------------------
HttpURLConnection urlConnection= (HttpURLConnection)((new URL
("http://www.testurl.com/doAction")).openConnection());
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setFollowRedirects(true);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF8");
urlConnection.setRequestProperty("HTTP-Version", "HTTP/1.1");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
// Posts to the url - Sends Query String
OutputStreamWriter l_wr = new OutputStreamWriter (
urlConnection.getOutputStream());
l_wr.write("query=123456");
l_wr.flush();
l_wr.close();
// Read lines from URL - Gets Response
BufferedReader l_buffin = new BufferedReader(new InputStreamReader (
urlConnection.getInputStream()));
while ((l_url_content = l_buffin.readLine()) != null) {
System.out.println(l_url_content);
}
urlConnection.disconnect();
------------------------------------------------------------------------
It seems that the InputStream and OutputStreams are are not made available.
Any ideas ?
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what the issue is, but if you need a quick fix, I'd suggest using the HttpClient at Apache's Jakarta site.
 
Anurag Gupta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a work around by using httpClient. If there is a problem, it should be solved.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anurag
I have done URLConnection based communication with Servlet/JSPs. So it should work for you as well. I am not sure why it doesn't work for you. Does it just
hang? Does it give you error after you wait for long? What error you see?
Please let us know what you are getting...
Regards
Maulin
 
Anurag Gupta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Response.
There are NO Errors. The program just hangs.... just after posting the query and while calling the
urlConnection.getInputStream()
to read the OutPut from the URL.
Dont know why this is happening.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you able to connect via Browser to that URL? I know you might be missing some parameters to post but if they are not many just get it work via GET request first to see if the URl works from the Browser ...
Now, if it works from the browser it should work from the program as well.
You can also try writing a smaller test servlet to which you make connection in similar manner and then see if it works. If that example works then you compare that test servlet with the original servlet (where URL connection hangs) and then try to find out differences that might lead you to the problem's root cause...
I guess there is only one way here- "go really step-by-step"...
Regards
Maulin
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try posting it to 'HTTP/1.0' instead of HTTP/1.1. I have seen similar behaviour in some cases.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic