Hi all,
Sorry about the beginner questions, but I'm trying to make a java program that sends a xml file to geoserver's wfs via http post, and I want to know if I'm doing the right thing. The test program should take a xml file as input argument, sends it to http://sigma.openplans.org:8080/geoserver/wfs, and print out the response. Here's the java program:
public class PostTest {
public static void main(String Args) throws Exception{
if (Args.length != 1)
throw new IllegalArgumentException("USAGE: input-xml-file-location");
URL site = new URL("http://sigma.openplans.org:8080/geoserver/wfs"\);
File queryFile = new File(Args[0]);
byte queryBytes = new byte[(int) queryFile.length()];
FileInputStream queryIn = new FileInputStream(queryFile);
queryIn.read(queryBytes);
queryIn.close();
System.out.println("Will Connect to site. . " + site);
System.out.println("Will Send Query File . . . " + queryFile);
HttpURLConnection connection = (HttpURLConnection) site.openConnection();
// now connection is configured write the queryFile contents to the outputStream
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
System.out.println("Connected to site.");
OutputStream out = connection.getOutputStream();
out.write(queryBytes);
// read response and write to System.out
System.out.println("Reading site's response:");
System.out.println();
InputStream in = new BufferedInputStream(connection.getInputStream(), 32 * 1024);
int character = in.read();
while (character != -1) {
System.out.print((char) character);
character = in.read();
}
System.out.println();
System.out.println("DONE. EXITING.");
}
}
the xml file is just the sample wfs getfeature xml on the demo:
<wfs:GetFeature service="WFS" version="1.0.0"
outputFormat="GML2"
xmlns:topp="http://www.openplans.org/topp"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs
http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
<wfs:Query typeName="topp:states">
<ogc:Filter>
<ogc:FeatureId fid="states.3"/>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>
and this is what I got back:
<?xml version="1.0" encoding="UTF-8"?>
<ows:ExceptionReport version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/ows http://sigma.openplans.org:8080/geoserver/schemas/ows/1.0.0/owsExceptionReport.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows">
<ows:Exception exceptionCode="MissingParameterValue" locator="request">
<ows:ExceptionText>Could not determine geoserver request from http request org.apache.coyote.tomcat5.CoyoteRequestFacade@anonymised.com</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
Am I doing the post request right? I also tried using URLEncoder class on the input, but I don't think that's right either...
Thanks in advance.
Doris
I believe what you're missing is setting the content type:
connection.setRequestProperty("Content-Type", "application/xml");
You can check out the code that we use for our sample requests in the demo section of the web admin tool:
http://svn.codehaus.org/geoserver/trunk/geoserver/wfs/src/main/java/org/vfny/geoserver/wfs/servlets/TestWfsPost.java
best regards,
Chris
Doris Lam wrote:
Hi all,
Sorry about the beginner questions, but I'm trying to make a java program that sends a xml file to geoserver's wfs via http post, and I want to know if I'm doing the right thing. The test program should take a xml file as input argument, sends it to http://sigma.openplans.org:8080/geoserver/wfs, and print out the response. Here's the java program:
public class PostTest {
public static void main(String Args) throws Exception{
if (Args.length != 1)
throw new IllegalArgumentException("USAGE: input-xml-file-location");
URL site = new URL("http://sigma.openplans.org:8080/geoserver/wfs"\);
File queryFile = new File(Args[0]);
byte queryBytes = new byte[(int) queryFile.length()];
FileInputStream queryIn = new FileInputStream(queryFile);
queryIn.read(queryBytes);
queryIn.close();
System.out.println("Will Connect to site. . " + site);
System.out.println("Will Send Query File . . . " + queryFile);
HttpURLConnection connection = (HttpURLConnection) site.openConnection();
// now connection is configured write the queryFile contents to the outputStream
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
System.out.println("Connected to site.");
OutputStream out = connection.getOutputStream();
out.write(queryBytes);
// read response and write to System.out
System.out.println("Reading site's response:");
System.out.println();
InputStream in = new BufferedInputStream(connection.getInputStream(), 32 * 1024);
int character = in.read();
while (character != -1) {
System.out.print((char) character);
character = in.read();
}
System.out.println();
System.out.println("DONE. EXITING.");
}
}
the xml file is just the sample wfs getfeature xml on the demo:
<wfs:GetFeature service="WFS" version="1.0.0"
outputFormat="GML2"
xmlns:topp="http://www.openplans.org/topp"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs
http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
<wfs:Query typeName="topp:states">
<ogc:Filter>
<ogc:FeatureId fid="states.3"/>
</ogc:Filter>
</wfs:Query>
</wfs:GetFeature>
and this is what I got back:
<?xml version="1.0" encoding="UTF-8"?>
<ows:ExceptionReport version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/ows http://sigma.openplans.org:8080/geoserver/schemas/ows/1.0.0/owsExceptionReport.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows">
<ows:Exception exceptionCode="MissingParameterValue" locator="request">
<ows:ExceptionText>Could not determine geoserver request from http request org.apache.coyote.tomcat5.CoyoteRequestFacade@anonymised.com</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
Am I doing the post request right? I also tried using URLEncoder class on the input, but I don't think that's right either...
Thanks in advance.
Doris
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Geoserver-users mailing list
Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users
!DSPAM:4005,4699996380487180515871!