在geoserver自定义的地图中通过geoserver wfs 查询,删除,添加相关的POI。

相关操作的格式如下:

查询

<wfs:GetFeature service="WFS" version="1.0.0"  
       outputFormat="GML2"  
       xmlns:opengis="http://www.cetusOpengis.com"  
       xmlns:wfs="http://www.opengis.net/wfs"  
       xmlns:ogc="http://www.opengis.net/ogc"  
       xmlns:gml="http://www.opengis.net/gml"  
       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="opengis:qqy">   
      <ogc:Filter>   
      <ogc:PropertyIsEqualTo>
      <ogc:PropertyName>status</ogc:PropertyName>
      <ogc:Literal>0</ogc:Literal>
      </ogc:PropertyIsEqualTo>
     </ogc:Filter>   
       </wfs:Query>   
    </wfs:GetFeature>;

添加

<wfs:Transaction service="WFS" version="1.0.0"  
       outputFormat="GML2"  
       xmlns:opengis="http://www.cetusOpengis.com"  
       xmlns:wfs="http://www.opengis.net/wfs"  
       xmlns:ogc="http://www.opengis.net/ogc"  
       xmlns:gml="http://www.opengis.net/gml"  
       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:Insert handle="someprj1">
          <opengis:someprj>
             <opengis:the_geom>
                <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#3395" >
                             <gml:coordinates decimal="." cs="," ts="">13404701.212,3850391.781</gml:coordinates>
                </gml:Point>
             </opengis:the_geom>
             <opengis:ssds>13</opengis:ssds>
             <opengis:qqybh>12</opengis:qqybh>
             <opengis:status>0</opengis:status>
          </opengis:someprj>
       </wfs:Insert>  
    </wfs:Transaction>;

修改

<wfs:Transaction service="WFS" version="1.0.0"  
       outputFormat="GML2"  
       xmlns:opengis="http://www.cetusOpengis.com"  
       xmlns:wfs="http://www.opengis.net/wfs"  
       xmlns:ogc="http://www.opengis.net/ogc"  
       xmlns:gml="http://www.opengis.net/gml"  
       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:Update typeName="opengis:qqyproject">
     <wfs:Property>  
       <wfs:Name>qqybh</wfs:Name>
       <wfs:Value>12</wfs:Value>
     </wfs:Property>
      <ogc:Filter>   
      <ogc:PropertyIsEqualTo>
      <ogc:PropertyName>qqybh</ogc:PropertyName>
      <ogc:Literal>0</ogc:Literal>
      </ogc:PropertyIsEqualTo>
     </ogc:Filter>   
       </wfs:Update>   
    </wfs:Transaction>;

删除

<wfs:Transaction service="WFS" version="1.0.0"  
       outputFormat="GML2"  
       xmlns:opengis="http://www.cetusOpengis.com"  
       xmlns:wfs="http://www.opengis.net/wfs"  
       xmlns:ogc="http://www.opengis.net/ogc"  
       xmlns:gml="http://www.opengis.net/gml"  
       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:Delete typeName="opengis:qqyproject">
      <ogc:Filter>   
      <ogc:PropertyIsLessThan>
      <ogc:PropertyName>qqybh</ogc:PropertyName>
      <ogc:Literal>12</ogc:Literal>
      </ogc:PropertyIsLessThan>
      <ogc:PropertyIsGreaterThan>
      <ogc:PropertyName>qqybh</ogc:PropertyName>
      <ogc:Literal>0</ogc:Literal>
      </ogc:PropertyIsGreaterThan>
     </ogc:Filter>   
       </wfs:Delete>   
    </wfs:Transaction>;

具体实现如下:

  1. package com.geoserver;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. /**
  6. * 操作geoserver的几种方法
  7. *
  8. *
  9. *
  10. * @Title: TODO
  11. * @Description: 实现TODO
  12. * @Copyright:Copyright (c) 2011
  13. * @Company:
  14. * @Date:2012-8-30
  15. * @author
  16. * @version 1.0
  17. */
  18. public class MainGeoService {
  19. public static void main(String[] args) {
  20. addGeoServerService();
  21. }
  22. /**
  23. * 添加地图定的信息
  24. */
  25. public static void addGeoServerService(){
  26. String layerName="loc_point";
  27. String namespaceValue="http://www.easyway.net.cn";
  28. double lat=139.54d;
  29. double lon=-116.23d;
  30. Map<String,String> params=new HashMap<String,String>();
  31. params.put("FID", "loc_point.4");
  32. params.put("NAME", "easyway_001");
  33. params.put("OBJECT_CODE", "beijing_tsingperk_768");
  34. params.put("HANDLE_ID", "768");
  35. params.put("STATUS", "1");
  36. params.put("DESCRIPTION", "this is point add by programe");
  37. StringBuffer sb = new StringBuffer();
  38. sb.append("<wfs:Transaction service='WFS' version='1.0.0'   ");
  39. sb.append("outputFormat='GML2'   ");
  40. sb.append(" xmlns:opengis='"+namespaceValue+"'   ");
  41. sb.append(" xmlns:wfs='http://www.opengis.net/wfs'   ");
  42. sb.append(" xmlns:ogc='http://www.opengis.net/ogc'   ");
  43. sb.append(" xmlns:gml='http://www.opengis.net/gml'   ");
  44. sb.append("xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'   ");
  45. sb.append(" xsi:schemaLocation='http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd'>   ");
  46. sb.append(" <wfs:Insert handle='"+layerName+"'>");
  47. sb.append("  <opengis:"+layerName+">");
  48. sb.append("    <opengis:the_geom>");
  49. sb.append("     <gml:Point srsName='http://www.opengis.net/gml/srs/epsg.xml#3395' >");
  50. sb.append("     <gml:coordinates decimal='.' cs=',' ts=' '>"+lat+","+lon+"</gml:coordinates>");
  51. sb.append("   </gml:Point>");
  52. sb.append(" </opengis:the_geom>");
  53. if(!params.isEmpty()){
  54. for (Entry<String,String> entry : params.entrySet()) {
  55. sb.append("      <opengis:"+entry.getKey()+">"+entry.getValue()+"</opengis:"+entry.getKey()+">");
  56. }
  57. }
  58. sb.append("  </opengis:"+layerName+">");
  59. sb.append(" </wfs:Insert>   ");
  60. sb.append(" </wfs:Transaction>");
  61. String response = HttpUtils.doGeoServerPOST(
  62. "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
  63. sb.toString());
  64. System.out.println("response:");
  65. System.out.println("" + response);
  66. }
  67. /**
  68. * 查询地图中的访问
  69. * 假定我们指定的查询范围为bbox,根据上述参数设定,进行范围查询的Url为:
  70. * “WfsUrl?REQUEST=GetFeature&typeName= WfsNamespace : WfsLayerName &bbox=bbox&outputFormat=json”。
  71. * bbox是怎么来的呢?bbox实际上描绘的是一个矩形,假定矩形左下角的点是Max(x1,y1),右上角的点是Min(x2,y2),
  72. * 则bbox是形如“x1,y1,x2,y2”的一个字符串。
  73. */
  74. public static void queryRangeGeoServerService() {
  75. //空间
  76. String namespace="jacob";
  77. //图层名称
  78. String layerName="loc_point";
  79. double minX=40.34d;
  80. double minY=65.344d;
  81. double maxX=45.34d;
  82. double maxY=67.34d;
  83. String queryRangeURL="http://10.100.108.20:8080/geoserver/wfs?REQUEST=GetFeature&typeName="+namespace+":"+layerName+"&bbox="+minX+","+minY+","+maxX+","+maxY+"&outputFormat=json";
  84. String response = HttpUtils.doGeoServerPOST(queryRangeURL,"");
  85. System.out.println("response:");
  86. System.out.println("" + response);
  87. }
  88. /**
  89. * 查询名称为cesuo 的地方
  90. *
  91. */
  92. public static void queryGeoServerService() {
  93. String namespace="jacob";
  94. String namespaceValue="http://www.easyway.net.cn";
  95. String layerName="loc_point";
  96. Map<String,String> params=new HashMap<String,String>();
  97. params.put("Name", "cesuo");
  98. StringBuffer sb = new StringBuffer();
  99. sb.append("<w:GetFeature service='WFS' version='1.1.0' ");
  100. sb.append(" xmlns:w='http://www.opengis.net/wfs' ");
  101. sb.append(" xmlns:f='"+namespaceValue+"' ");
  102. sb.append(" xmlns:g='http://www.opengis.net/gml' ");
  103. sb.append(" xmlns:o='http://www.opengis.net/ogc' ");
  104. sb.append(" x:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd WfsUrl/DescribeFeatureType?version=1.1.0&amp;typename="+namespace+":"+layerName+"' ");
  105. sb.append(" xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>");
  106. sb.append(" <w:Query typeName='f:"+layerName+"'>");
  107. sb.append("  <o:Filter>");
  108. sb.append("     <o:PropertyIsEqualTo>");
  109. if(!params.isEmpty()){
  110. for (Entry<String,String> entry : params.entrySet()) {
  111. sb.append("      <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>");
  112. sb.append("       <o:Literal>"+entry.getValue()+"</o:Literal>");
  113. }
  114. }
  115. sb.append("   </o:PropertyIsEqualTo>");
  116. sb.append(" </o:Filter>");
  117. sb.append(" </w:Query>");
  118. sb.append("</w:GetFeature>");
  119. String response = HttpUtils.doGeoServerPOST(
  120. "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
  121. sb.toString());
  122. System.out.println("response:");
  123. System.out.println("" + response);
  124. }
  125. /**
  126. *
  127. * 假定我们要删除的STATE_NAME为北京的点,则根据上述参数设定,此查询的url为:WfsUrl,
  128. * 同时需要将如下形式的参数信息,提交到服务器。如以post的方式,
  129. * 将参数信息写入HttpWebRequest的RequestStream中。
  130. *
  131. */
  132. public static void deletePointGeoServerService() {
  133. String namespace="jacob";
  134. String namespaceValue="http://www.easyway.net.cn";
  135. String layerName="loc_point";
  136. Map<String,String> params=new HashMap<String,String>();
  137. params.put("Name", "cesuo");
  138. StringBuffer sb = new StringBuffer();
  139. sb.append("<w:Transaction xmlns:w='http://www.opengis.net/wfs' ");
  140. sb.append(" xmlns:f='"+namespaceValue+"' xmlns:g='http://www.opengis.net/gml' ");
  141. sb.append("  service='WFS' version='1.1.0' xmlns:o='http://www.opengis.net/ogc' ");
  142. sb.append("  x:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd WfsUrl /DescribeFeatureType?version=1.1.0&amp;typename="+namespace+":"+layerName+" ' xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>");
  143. sb.append("   <w:Delete typeName='f:"+layerName+"'>");
  144. sb.append("  <o:Filter>");
  145. sb.append("    <o:PropertyIsEqualTo>");
  146. if(!params.isEmpty()){
  147. for (Entry<String,String> entry : params.entrySet()) {
  148. sb.append("      <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>");
  149. sb.append("       <o:Literal>"+entry.getValue()+"</o:Literal>");
  150. }
  151. }
  152. sb.append("   </o:PropertyIsEqualTo>");
  153. sb.append("        </o:Filter>");
  154. sb.append("     </w:Delete>");
  155. sb.append(" </w:Transaction>");
  156. String response = HttpUtils.doGeoServerPOST(
  157. "http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
  158. sb.toString());
  159. System.out.println("response:");
  160. System.out.println("" + response);
  161. }
  162. }
package com.geoserver;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry; /**
* 操作geoserver的几种方法
*
*
*
* @Title: TODO
* @Description: 实现TODO
* @Copyright:Copyright (c) 2011
* @Company:
* @Date:2012-8-30
* @author
* @version 1.0
*/
public class MainGeoService { public static void main(String[] args) {
addGeoServerService(); }
/**
* 添加地图定的信息
*/
public static void addGeoServerService(){
String layerName="loc_point";
String namespaceValue="http://www.easyway.net.cn";
double lat=139.54d;
double lon=-116.23d; Map<String,String> params=new HashMap<String,String>();
params.put("FID", "loc_point.4");
params.put("NAME", "easyway_001");
params.put("OBJECT_CODE", "beijing_tsingperk_768");
params.put("HANDLE_ID", "768");
params.put("STATUS", "1");
params.put("DESCRIPTION", "this is point add by programe"); StringBuffer sb = new StringBuffer();
sb.append("<wfs:Transaction service='WFS' version='1.0.0' ");
sb.append("outputFormat='GML2' ");
sb.append(" xmlns:opengis='"+namespaceValue+"' ");
sb.append(" xmlns:wfs='http://www.opengis.net/wfs' ");
sb.append(" xmlns:ogc='http://www.opengis.net/ogc' ");
sb.append(" xmlns:gml='http://www.opengis.net/gml' ");
sb.append("xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ");
sb.append(" xsi:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd'> ");
sb.append(" <wfs:Insert handle='"+layerName+"'>");
sb.append(" <opengis:"+layerName+">");
sb.append(" <opengis:the_geom>");
sb.append(" <gml:Point srsName='http://www.opengis.net/gml/srs/epsg.xml#3395' >");
sb.append(" <gml:coordinates decimal='.' cs=',' ts=' '>"+lat+","+lon+"</gml:coordinates>");
sb.append(" </gml:Point>");
sb.append(" </opengis:the_geom>"); if(!params.isEmpty()){
for (Entry<String,String> entry : params.entrySet()) {
sb.append(" <opengis:"+entry.getKey()+">"+entry.getValue()+"</opengis:"+entry.getKey()+">");
}
}
sb.append(" </opengis:"+layerName+">");
sb.append(" </wfs:Insert> ");
sb.append(" </wfs:Transaction>");
String response = HttpUtils.doGeoServerPOST(
"http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
sb.toString());
System.out.println("response:");
System.out.println("" + response);
}
/**
* 查询地图中的访问
* 假定我们指定的查询范围为bbox,根据上述参数设定,进行范围查询的Url为:
* “WfsUrl?REQUEST=GetFeature&typeName= WfsNamespace : WfsLayerName &bbox=bbox&outputFormat=json”。
* bbox是怎么来的呢?bbox实际上描绘的是一个矩形,假定矩形左下角的点是Max(x1,y1),右上角的点是Min(x2,y2),
* 则bbox是形如“x1,y1,x2,y2”的一个字符串。
*/
public static void queryRangeGeoServerService() {
//空间
String namespace="jacob";
//图层名称
String layerName="loc_point"; double minX=40.34d;
double minY=65.344d;
double maxX=45.34d;
double maxY=67.34d; String queryRangeURL="http://10.100.108.20:8080/geoserver/wfs?REQUEST=GetFeature&typeName="+namespace+":"+layerName+"&bbox="+minX+","+minY+","+maxX+","+maxY+"&outputFormat=json";
String response = HttpUtils.doGeoServerPOST(queryRangeURL,"");
System.out.println("response:");
System.out.println("" + response);
}
/**
* 查询名称为cesuo 的地方
*
*/
public static void queryGeoServerService() {
String namespace="jacob";
String namespaceValue="http://www.easyway.net.cn";
String layerName="loc_point";
Map<String,String> params=new HashMap<String,String>();
params.put("Name", "cesuo"); StringBuffer sb = new StringBuffer();
sb.append("<w:GetFeature service='WFS' version='1.1.0' ");
sb.append(" xmlns:w='http://www.opengis.net/wfs' ");
sb.append(" xmlns:f='"+namespaceValue+"' ");
sb.append(" xmlns:g='http://www.opengis.net/gml' ");
sb.append(" xmlns:o='http://www.opengis.net/ogc' ");
sb.append(" x:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd WfsUrl/DescribeFeatureType?version=1.1.0&amp;typename="+namespace+":"+layerName+"' ");
sb.append(" xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>");
sb.append(" <w:Query typeName='f:"+layerName+"'>");
sb.append(" <o:Filter>");
sb.append(" <o:PropertyIsEqualTo>");
if(!params.isEmpty()){
for (Entry<String,String> entry : params.entrySet()) {
sb.append(" <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>");
sb.append(" <o:Literal>"+entry.getValue()+"</o:Literal>");
}
}
sb.append(" </o:PropertyIsEqualTo>");
sb.append(" </o:Filter>");
sb.append(" </w:Query>");
sb.append("</w:GetFeature>");
String response = HttpUtils.doGeoServerPOST(
"http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
sb.toString());
System.out.println("response:");
System.out.println("" + response);
} /**
*
* 假定我们要删除的STATE_NAME为北京的点,则根据上述参数设定,此查询的url为:WfsUrl,
* 同时需要将如下形式的参数信息,提交到服务器。如以post的方式,
* 将参数信息写入HttpWebRequest的RequestStream中。
*
*/
public static void deletePointGeoServerService() { String namespace="jacob";
String namespaceValue="http://www.easyway.net.cn";
String layerName="loc_point";
Map<String,String> params=new HashMap<String,String>();
params.put("Name", "cesuo"); StringBuffer sb = new StringBuffer();
sb.append("<w:Transaction xmlns:w='http://www.opengis.net/wfs' ");
sb.append(" xmlns:f='"+namespaceValue+"' xmlns:g='http://www.opengis.net/gml' ");
sb.append(" service='WFS' version='1.1.0' xmlns:o='http://www.opengis.net/ogc' ");
sb.append(" x:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd WfsUrl /DescribeFeatureType?version=1.1.0&amp;typename="+namespace+":"+layerName+" ' xmlns:x='http://www.w3.org/2001/XMLSchema-instance'>");
sb.append(" <w:Delete typeName='f:"+layerName+"'>");
sb.append(" <o:Filter>");
sb.append(" <o:PropertyIsEqualTo>");
if(!params.isEmpty()){
for (Entry<String,String> entry : params.entrySet()) {
sb.append(" <o:PropertyName>f:"+entry.getKey()+"</o:PropertyName>");
sb.append(" <o:Literal>"+entry.getValue()+"</o:Literal>");
}
}
sb.append(" </o:PropertyIsEqualTo>");
sb.append(" </o:Filter>");
sb.append(" </w:Delete>");
sb.append(" </w:Transaction>");
String response = HttpUtils.doGeoServerPOST(
"http://10.100.108.20:8080/geoserver/wfs?outputFormat=json",
sb.toString());
System.out.println("response:");
System.out.println("" + response);
}
}
http工具类:
  1. package com.geoserver;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import org.apache.http.Header;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpHost;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.conn.scheme.PlainSocketFactory;
  11. import org.apache.http.conn.scheme.Scheme;
  12. import org.apache.http.conn.scheme.SchemeRegistry;
  13. import org.apache.http.conn.ssl.SSLSocketFactory;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.params.CoreConnectionPNames;
  17. import org.apache.http.util.EntityUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. /**
  21. *
  22. * @Title: TODO
  23. * @Description: 实现TODO
  24. * @Copyright:Copyright (c) 2011
  25. * @Company:
  26. * @Date:2012-8-30
  27. * @author
  28. * @version 1.0
  29. */
  30. public class HttpUtils {
  31. private final static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
  32. private static final int    DEFAULT_CONN_TIMEOUT_MILLISECONDS   = 5 * 1000;
  33. private static final int    DEFAULT_READ_TIMEOUT_MILLISECONDS   = 60 * 1000;
  34. private static final String CHARSET = "utf-8";
  35. private static HttpClient httpClient;
  36. static HttpHost proxy;
  37. static {
  38. proxy = new HttpHost("10.0.0.172", 80);
  39. }
  40. private static HttpClient initHttpClient(String charset) {
  41. if (charset == null)
  42. charset = System.getProperty("sun.jnu.encoding");
  43. releaseConnection();
  44. // 设置我们的HttpClient支持HTTP和HTTPS两种模式
  45. SchemeRegistry schemeRegistry = new SchemeRegistry();
  46. schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
  47. schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
  48. // 使用线程安全的连接管理来创建HttpClient
  49. /*ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
  50. connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
  51. connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);*/
  52. httpClient = new DefaultHttpClient();
  53. /*HttpParams params = httpClient.getParams();
  54. params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
  55. DEFAULT_CONN_TIMEOUT_MILLISECONDS);
  56. params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
  57. DEFAULT_READ_TIMEOUT_MILLISECONDS);*/
  58. setConnectTimeout(DEFAULT_CONN_TIMEOUT_MILLISECONDS);
  59. setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
  60. return httpClient;
  61. }
  62. /**
  63. * HTTP post请求
  64. *
  65. * @param url
  66. *            请求地址
  67. * @param parmsMap
  68. *            post数据
  69. * @return
  70. */
  71. public static String doGeoServerPOST(String url, String requst) {
  72. HttpPost httpPost = null;
  73. HttpResponse httpResponse = null;
  74. ObjectInputStream ois = null;
  75. try {
  76. if (httpClient == null)
  77. initHttpClient(CHARSET);
  78. httpPost = new HttpPost(url);
  79. // 绑定参数Entity
  80. StringEntity stringEntity = new StringEntity(requst, CHARSET);
  81. httpPost.setEntity(stringEntity);
  82. // 发送请求
  83. httpResponse = httpClient.execute(httpPost);
  84. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  85. Header[] headers = httpResponse.getAllHeaders();
  86. for(Header h : headers){
  87. System.err.println(h.getName() + " : " + h.getValue());
  88. }
  89. HttpEntity entity = httpResponse.getEntity();
  90. return EntityUtils.toString(entity);
  91. }
  92. return null;
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. return null;
  96. } finally {
  97. try {
  98. if (ois != null) {
  99. ois.close();
  100. }
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. httpClient.getConnectionManager().shutdown();
  105. }
  106. }
  107. /**
  108. * Set the connection timeout for the underlying HttpClient. A timeout value
  109. * of 0 specifies an infinite timeout.
  110. *
  111. * @param timeout the timeout value in milliseconds
  112. */
  113. private static void setConnectTimeout(int timeout) {
  114. httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
  115. timeout);
  116. }
  117. /**
  118. * Set the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout
  119. * for waiting for data or, put differently, a maximum period inactivity
  120. * between two consecutive data packets.A timeout value of 0 specifies an
  121. * infinite timeout.
  122. *
  123. * @param timeout the timeout value in milliseconds
  124. */
  125. private static void setReadTimeout(int timeout) {
  126. httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
  127. }
  128. private static void releaseConnection() {
  129. if (httpClient != null)
  130. httpClient.getConnectionManager().shutdown();
  131. }
  132. }
05-11 13:08