473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help: URLConnection to webservice

Hello,

I am trying to submit a request object to a server (not local), using
a servlet. When given a ZIP code this particular server is supposed to
return xml with the city/state/areacode etc... The instructions for
the server are:

:HTTP GET
The following is a sample HTTP GET request and response.

GET /uszip.asmx/GetInfoByZIP?USZip=string HTTP/1.1
Host: www.webservicex.net

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml
I am trying to implement this with:
================================================== ==========================

package com.banner;

import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ZipGet extends HttpServlet
{

public void doPost(HttpServletRequest req, HttpServletResponse rep)
throws IOException, ServletException
{
String fwrd = "http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=92101";
URL u = new URL(fwrd);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
OutputStream out = connection.getOutputStream();

out.flush();
out.close();

InputStream in = connection.getInputStream();
String retdata = in.toString();
in.close();
connection.disconnect();

}
}
================================================== ========================
When I try to run the servlet Tomcat gives me an error:
HTTP Status 405 - HTTP method GET is not supported by this URL.

However I know the server accepts "GET" requests, because I can just
type:
http://www.webservicex.net/uszip.asm...IP?USZip=92101
and I get what I'm looking for. Can anyone tell me if I am way off
base in my approach? Can anyone tell me what I am doing wrong?

I appreciate any help that anyone can give.
Jul 17 '05 #1
1 9685
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Johnnie wrote:
package com.banner;

import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ZipGet extends HttpServlet
{

public void doPost(HttpServletRequest req, HttpServletResponse rep)
throws IOException, ServletException
{
String fwrd =
"http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=92101";
URL u = new URL(fwrd); URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
OutputStream out = connection.getOutputStream();

out.flush();
out.close();

InputStream in = connection.getInputStream();
String retdata = in.toString();
in.close();
connection.disconnect();

}
}


Hi,
I haven't tried anything, but I have a pretty good idea what the
problem is. You're using the GET method, so all parameters are
encoded into the URL itself. You should probably call
setDoOutput(false); and eliminate the now-useless code further down.
I don't know if this will make any difference, but I believe that one
should always use setDoOutput(false) when using the GET method.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/Gye2wxczzJRavJYRAip/AJ47+9qj45E4byEAsuTGY1AWlK0TWACfd7BX
ST0mZRyoBK9fibOGgdjhNV0=
=2uqK
-----END PGP SIGNATURE-----
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jochen Daum | last post by:
Hi, I have to emulate a "file upload" to a Java Servlet which is done with the class URLConnection. The java source basically does URLConnection conn =...
0
by: prabhat | last post by:
Hi, The getHeaderField(0) of class URLConnection in java.net package returns a String which provides status code. An example of returned string is : "HTTP/1.0 200 OK". The status code 200 mean...
2
by: Stacey | last post by:
Here is a function that is declared in my html page. It is part of a series of javascript that allows the user to draw a rectangle on a graphic. This is called when the box is done, so I would...
1
by: David | last post by:
Hi All, I am having trouble cleaning up after creating/calling my webservice (WSE1.0) from COM Addin that I create for Outlook. If I shutdown Outlook with using my webservice, Outlook shutsdown...
0
by: batista | last post by:
Hello all, I am using a webpage having a grid and im calling a webservice method(using webservice.htc) which after 5 seconds updates the content of the grid, uptil now everything is working...
1
by: smerf | last post by:
I am really new to this .Net stuff, but I am wondering if you can help me with a webservice issue. I have 15 locations that need to access a webservice. The webservice accepts XML and returns...
4
by: John Sitka | last post by:
Hi, sorry for a crosspost but that other news group was showing last post was a week ago so I guess it dosen't see much use... I'm about to start a solution and I'm curious about the approach of...
3
by: splintercell | last post by:
well i got this code from java.sun.com and tried modiifying it in all the possible ways,but to no good.. stil its not workin..pleas help me out and try postin good workinw web cralwer if u have.....
0
by: Jeova Almeida | last post by:
Hello, I created a simple web service for testing purpose in Visual Studio 2005: <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="WSTest" %> using System;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.