Java连接WebServce

来源:互联网 发布:抗风柱设计软件 编辑:程序博客网 时间:2024/06/06 08:21
package org.external.WeChatBill;  import java.io.IOException;  import net.sf.json.JSONObject;import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;  public class postjson {      public static String sendInfo(String sendurl, String data) {         //System.out.println("data:"+data);        CloseableHttpClient client = HttpClients.createDefault();         HttpPost post = new HttpPost(sendurl);        StringEntity myEntity = new StringEntity(data,                 ContentType.APPLICATION_JSON);         post.setEntity(myEntity);         String responseContent = null;          CloseableHttpResponse response = null;         try {           response = client.execute(post);            if (response.getStatusLine().getStatusCode() == 200) {                 HttpEntity entity = response.getEntity();                 responseContent = EntityUtils.toString(entity, "UTF-8");                 String aa = null;             }         } catch (ClientProtocolException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } finally {             try {                if (response != null)                    response.close();              } catch (IOException e) {                 e.printStackTrace();             } finally {                 try {                     if (client != null)                         client.close();                 } catch (IOException e) {                     e.printStackTrace();                 }             }         }        return responseContent;     }