使用httpclient 连接 restful webservices实例 绝对可用 get的--post
2014-10-21 17:33
561 查看
package org.glassfish.jersey.examples.httpclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class ApacheHttpClientGet { public static void main(String[] args) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet( // "http://localhost:8080/RESTfulExample/json/product/get"); "http://10.16.232.79:8555/base/helloworld/h"); // getRequest.addHeader("accept", "application/json"); getRequest.addHeader("accept", "text/plain"); HttpResponse response = httpClient.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
post:
<pre name="code" class="html">package org.glassfish.jersey.examples.httpclient;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URI;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicHeader;import org.apache.http.protocol.HTTP;public class ApacheHttpClientPost {public static void main(String[] args) {ApacheHttpClientPost post = new ApacheHttpClientPost();try {// post.doPost("http://localhost:8080/RESTfulExample/json/product/post", "{\"qty\":100,\"name\":\"iPad 4\"}");// post.doPost("http://1sss:8555/base/helloworld/post", "{\"qty\":100,\"name\":\"iPad 4\"}");post.doPost("http://abc/def", "{\"sort\":100,\"name\":\"iPad 4\"}");} catch (Exception e) {e.printStackTrace();}/*try {DefaultHttpClient httpClient = new DefaultHttpClient();HttpPost postRequest = new HttpPost("http://10.16.232.79:8555/base/helloworld/post");StringEntity input = new StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}");input.setContentType("application/json");postRequest.setEntity(input);HttpResponse response = httpClient.execute(postRequest);if (response.getStatusLine().getStatusCode() != 201) {throw new RuntimeException("Failed : HTTP error code : "+ response.getStatusLine().getStatusCode());}BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));String output;System.out.println("Output from Server .... \n");while ((output = br.readLine()) != null) {System.out.println(output);}httpClient.getConnectionManager().shutdown();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}*/}// public String doPost(String wcfUrl,JSONObject jsonObject) throws// Exception {public String doPost(String wcfUrl, String jsonObject) throws Exception {HttpClient httpClient = new DefaultHttpClient();HttpResponse response;HttpPost post = new HttpPost();HttpEntity httpEntity;StringEntity stringEntity = new StringEntity(jsonObject.toString());stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));httpEntity = stringEntity;post.setEntity(httpEntity);post.setURI(new URI(wcfUrl));post.setHeader("Content-type", "application/json");response = httpClient.execute(post);return parseHttpResponse(response);}public String parseHttpResponse(HttpResponse response) throws Exception {String jsonString = "";int status = response.getStatusLine().getStatusCode();if (status == 200) {BufferedReader bReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuffer sb = new StringBuffer("");String line = "";String NL = System.getProperty("line.separator");while ((line = bReader.readLine()) != null) {sb.append(line + NL);}jsonString = sb.toString();bReader.close();}return jsonString;}}
相关文章推荐
- Android学习笔记---27_网络通信之通过GET和POST方式提交参数给web应用,以及使用httpClient,来给web项目以post方式发送参数
- jax-rs(Java API for RESTful Web Services)实践教程 之二 —— get/post/put/delete/head
- 一个使用HttpClient get post请求的实例
- Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)
- jax-rs(Java API for RESTful Web Services)实践教程 之二 —— get/post/put/delete/head
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- 使用 Spring 3 来创建 RESTful Web Services
- [Android开发]Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Android中使用HttpClient 服务 GET,POST和上传
- ajax 实现 GET POST 异步 同步 检验mysql连接情况简单实例
- Android HTTP实例 使用GET方法和POST方法发送请求
- HttpClient使用Post和Get提交参数
- android中使用get、post和HttpClient方式与服务器交互
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- Android HTTP实例 使用GET方法和POST方法发送请求
- 使用get,post,httpclient三种方式向服务器提交文本数据
- Android使用HttpClient完成Post和Get方式的联网请求
- 使用get,post,httpclient三种方式向服务器提交文本数据