日期:2014-05-17 浏览次数:20813 次
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
String surl = "http://192.168.205.73:8399/arcgis/rest/services/gisrn/MapServer/0/query?f=json&outSR=21480&returnDistinctValues=false&returnGeometry=true&where=FLDCNAME+like+%27%25%E7%83%9F%E5%8F%B0%25%27&spatialRel=esriSpatialRelIntersects&outFields=FLDID%2CFLDTAG%2CFLDCNAME%2COBJECTID";
//String surl = "http://192.168.205.73:8399/arcgis/rest/services/gisrn/MapServer/0/query?f=json&outSR=21480&returnDistinctValues=false&returnGeometry=true&where=FLDCNAME+like+%27%25116%25%27&spatialRel=esriSpatialRelIntersects&outFields=FLDID%2CFLDTAG%2CFLDCNAME%2COBJECTID";
URL url = new URL(surl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println("Received a : " + con.getClass().getName());
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
String msg = "Hi HTTP SERVER! Just a quick hello!";
con.setRequestProperty("CONTENT_LENGTH", "" + msg.length());
// Not checked
System.out.println("Msg Length: " + msg.length());
System.out.println("Getting an output stream...");
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(msg);
osw.flush();
osw.close();
System.out.println("After flushing output stream. ");
System.out.println("Getting an input stream...");
InputStream is = con.getInputStream();
// any response?
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("line: " + line);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}