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.