当前位置: 首页 > news >正文

扬州建设教育信息网站建大型门户网站

扬州建设教育信息网站,建大型门户网站,上海连锁设计公司,怎么自己做网站吗如今#xff0c;大多数用于与某些服务器通信的移动应用程序都使用REST服务。 这些服务也是与JavaScript或jQuery一起使用的常见做法。 现在#xff0c;我知道在Java中为REST服务创建客户端的2种方法#xff0c;在本文中#xff0c;我将尝试演示这两种方法#xff0c;希望它… 如今大多数用于与某些服务器通信的移动应用程序都使用REST服务。 这些服务也是与JavaScript或jQuery一起使用的常见做法。 现在我知道在Java中为REST服务创建客户端的2种方法在本文中我将尝试演示这两种方法希望它们能以某种方式对某人有所帮助。 1.使用Apache HttpClient Apache HttpClient库简化了HTTP请求的处理。 要使用此库您必须从其网站下载具有相关性的二进制文件。 这是HTTP GET方法的代码 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.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient client new DefaultHttpClient();HttpGet request new HttpGet(http://restUrl);HttpResponse response client.execute(request);BufferedReader rd new BufferedReader (new InputStreamReader(response.getEntity().getContent()));String line ;while ((line rd.readLine()) ! null) {System.out.println(line);}} } 对于Post方法 用于在帖子中发送简单的字符串 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.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient client new DefaultHttpClient();HttpPost post new HttpPost(http://restUrl);StringEntity input new StringEntity(product);post.setEntity(input);HttpResponse response client.execute(post);BufferedReader rd new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line ;while ((line rd.readLine()) ! null) {System.out.println(line);}} } 您还可以通过以下方式发送POJO的完整JSON或XML 将表示JSON或XML的String用作StringEntity的参数然后设置输入内容类型。 像这样 StringEntity input new StringEntity({\name1\:\value1\,\name2\:\value2\}); //here instead of JSON you can also have XML input.setContentType(application/json); 对于JSON您可以使用JSONObject创建JSON的字符串表示形式。 JSONObject json new JSONObject(); json.put(name1, value1); json.put(name2, value2); StringEntity se new StringEntity( json.toString()); 并在后请求中发送多个参数 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient client new DefaultHttpClient();HttpPost post new HttpPost(http://restUrl);List nameValuePairs new ArrayList(1);nameValuePairs.add(new BasicNameValuePair(name, value)); //you can as many name value pair as you want in the list.post.setEntity(new UrlEncodedFormEntity(nameValuePairs));HttpResponse response client.execute(post);BufferedReader rd new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line ;while ((line rd.readLine()) ! null) {System.out.println(line);}} } 2.使用球衣 Jersey是JSR-311规范Java中REST支持的规范的参考实现。 泽西岛基本上包含一个REST服务器和一个REST客户端。 它提供了一个库来与产生REST服务的服务器通信。 对于http get方法 import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.apache.http.client.ClientProtocolException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {ClientConfig config new DefaultClientConfig();Client client Client.create(config);WebResource service client.resource(UriBuilder.fromUri(http://restUrl).build());// getting XML dataSystem.out.println(service. path(restPath).path(resourcePath).accept(MediaType.APPLICATION_JSON).get(String.class));// getting JSON dataSystem.out.println(service. path(restPath).path(resourcePath).accept(MediaType.APPLICATION_XML).get(String.class));} } 您还可以通过其他媒体格式获得响应例如PLAIN或HTML。 对于HTTP POST方法 import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; import org.apache.http.client.ClientProtocolException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.core.util.MultivaluedMapImpl; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {ClientConfig config new DefaultClientConfig();Client client Client.create(config);WebResource webResource client.resource(UriBuilder.fromUri(http://restUrl).build());MultivaluedMap formData new MultivaluedMapImpl();formData.add(name1, val1);formData.add(name2, val2);ClientResponse response webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);System.out.println(Response response.getEntity(String.class));} } 如果您在POST中使用POJO则可以执行以下操作 ClientResponse response webResource.path(restPath).path(resourcePath). type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, myPojo); System.out.println(Response response.getEntity(String.class)); 这里的myPojo是自定义POJO类的实例。 您还可以使用Jersey的Form类在POST请求中提交多个参数 import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.apache.http.client.ClientProtocolException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.representation.Form; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {ClientConfig config new DefaultClientConfig();Client client Client.create(config);WebResource service client.resource(UriBuilder.fromUri(http://restUrl).build());Form form new Form();form.add(name1, value1);form.add(name2, value1);ClientResponse response service.path(restPath).path(resourcePath).type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);System.out.println(Response response.getEntity(String.class));} } 祝您编程愉快别忘了分享 参考来自harryjoy博客的JCG合作伙伴 Harsh Raval的Java中的简单REST客户端 。 翻译自: https://www.javacodegeeks.com/2012/09/simple-rest-client-in-java.html
http://www.zqtcl.cn/news/895250/

相关文章:

  • 网站内链怎么优化e时代网站制作
  • 记事本做网站素材代码国内十大4a广告公司
  • 一米八效果图网站商业网站平台
  • 做搜狗手机网站优化产品推广计划怎么写
  • 网站链接优化怎么做ftp服务器
  • 什么网站可以接单做海报网站信息员队伍建设方案
  • 淘宝联盟 网站怎么做网站运营推广方案设计
  • 网站建设数据库类型百度seo现状
  • 德州网站优化公司平面设计公司企业logo设计
  • 山东平台网站建设价位网站广告文案
  • 可以做哪方面的网站万网董事长是谁
  • 京东网站开发费用程序员找工作的网站
  • 怎么做网站首页psdwordpress 注册验证
  • 商丘做网站的公司有哪些郑州网站公司排名
  • 竞价网站与竞价网站之间做友情链接企业邮箱查询
  • 国外jquery网站wordpress 下一页 模板
  • 安卓手机做网站云南建设厅网站职称评定
  • 国外域名注册商网站邮箱登陆登录入口
  • 男女做那个的网站是什么深圳市8号公告
  • 做网站收款支付宝接口廊坊市网站建设公司
  • 文档下载网站 建设做cpa用什么网站
  • 网站制作合同注意事项百度网页版电脑版
  • 怎样做模板网站手机营销型网站制作
  • 如何采集网站内容如何做网站导航栏的搜索引擎优化
  • 网站关键词排名外包织梦大气婚纱影楼网站源码
  • 网站建设执行力冠县哪里有做网站的
  • 免费网站推广咱们做网络营销推广的应用场景
  • 深圳正规网站制作哪家公司好做网站代理属于开设赌场罪吗
  • 江西宜春市建设局网站wordpress博客下载器
  • 汕头站扩建效果图微信怎么引流营销呢