永发信息网

如何用get方post方式向http接口发送数据

答案:2  悬赏:0  手机版
解决时间 2021-03-27 04:26
如何用get方post方式向http接口发送数据
最佳答案
//添加http头信息httppost.addHeader(Authorization,yourtoken);//认证tokenhttppost.addHeader(Content-Type,application/json);httppost.addHeader(User-Agent,imgfornote);//httppost的json数据格式:{name:yourname,parentId:id_of_parent}JSONObjectobj=newJSONObject();obj.put(name,yourname);obj.put(parentId,yourparentid);httppost.setEntity(newStringEntity(obj.toString()));HttpResponseresponse;response=httpclient.execute(httppost);//检验状态码,如果成功接收数据intcode=response.getStatusLine().getStatusCode();if(code==200){Stringrev=EntityUtils.toString(response.getEntity());//返回json格式:{id:27JpL~j4vsL0LX00E00005,version:abc}obj=newJSONObject(rev);Stringid=obj.getString(id);Stringversion=obj.getString(version);}}catch(ClientProtocolExceptione){}catch(Exceptione){}}主要用到的类有:org.apache.http.client.HttpClient、org.apache.http.client.methods.HttpPost和org.json.JSONObject
全部回答
本节摘要:本节主要分别介绍如何用get方式、post方式向http接口发送数据。 preparation 1. 项目环境如下: myeclipse6.5 、tomcat5.0、system:xp、jdk:开发1.5,编译1.4 为了方便,在原来的web项目updown中新建了一个httpcall包,用来保存http接口和调用的客户端。 2.准备需要的jar包 * commons-httpclient-3.0.jar * commons-logging.jar * commons-codec-1.3.jar 3.class&method httpclient: getmethod: postmethod: start 接口写了一个servlet来接收客户端get/post的请求 web.xml需要加入以下配置: testhttpserver httpcall.testhttpserver testhttpserver /httpserver testhttpserver.java的代码如下: testhttpserver 1 package httpcall; 2 3 import java.io.ioexception; 4 import java.io.printwriter; 5 import javax.servlet.servletexception; 6 import javax.servlet.http.httpservlet; 7 import javax.servlet.http.httpservletrequest; 8 import javax.servlet.http.httpservletresponse; 9 10 17 18 public class testhttpserver extends httpservlet{ 19 20 private static final long serialversionuid = 1l; 22 public void doget(httpservletrequest request, httpservletresponse response) 23 throws servletexception, ioexception { 24 response.setcharacterencoding("gbk"); 25 26 printwriter out = response.getwriter(); 27 string param1 = request.getparameter("param1"); 28 out.println("param1=" + param1); 29 string param2 = request.getparameter("param2"); 30 out.println("param2=" + param2); 31 if (param1 == null || "".equals("param1") || param1.length() <= 0) { 32 out.println("http call failed,参数param1不能为空,程序退出"); 33 } else if (param2 == null || "".equals("param2") 34 || param2.length() <= 0) { 35 out.println("http call failed,参数param2不能为空,程序退出"); 36 } else { 37 out.println("---http call success---"); 38 } 39 out.close(); 40 } 41 42 public void dopost(httpservletrequest request, httpservletresponse response) 43 throws servletexception, ioexception { 44 this.doget(request, response); 45 } 46 } httpclientutil.java的代码如下: httpclientutil 1 package httpcall; 3 import java.io.ioexception; 4 import org.apache.commons.httpclient.defaulthttpmethodretryhandler; 5 //import org.apache.commons.httpclient.header; 6 import org.apache.commons.httpclient.httpclient; 7 import org.apache.commons.httpclient.httpexception; 8 //import org.apache.commons.httpclient.httpstatus; 9 import org.apache.commons.httpclient.methods.getmethod; 10 import org.apache.commons.httpclient.methods.postmethod; 11 import org.apache.commons.httpclient.params.httpmethodparams; 12 //import org.apache.commons.httpclient.namevaluepair; 13 import org.apache.commons.logging.log; 14 import org.apache.commons.logging.logfactory; 15 16 24 25 public class httpclientutil { 26 27 private static final log log = logfactory 28 .getlog(httpclientutil.class); 29 30 36 public static string gethttp(string param1,string param2){ 37 string responsemsg = ""; 38 39 // 1.构造httpclient的实例 40 httpclient httpclient = new httpclient(); 41 42 // 用于测试的http接口的url 43 string url="http://localhost:8080/updown/httpserver?param1="+param1+"¶m2="+param2; 44 45 // 2.创建getmethod的实例 46 getmethod getmethod = new getmethod(url); 47 48 // 使用系统系统的默认的恢复策略 49 getmethod.getparams().setparameter(httpmethodparams.retry_handler, 50 new defaulthttpmethodretryhandler()); 51 52 try { 53 //3.执行getmethod,调用http接口 54 httpclient.executemethod(getmethod); 55 56 //4.读取内容 57 byte[] responsebody = getmethod.getresponsebody(); 58 59 //5.处理返回的内容 60 responsemsg = new string(responsebody); 61 log.info(responsemsg); 62 63 } catch (httpexception e) { 64 e.printstacktrace(); 65 } catch (ioexception e) {
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
用黄豆500克自制纳豆放纳豆菌多少才适合
房子是用于住好还是投资好
河北师范大学专科好吗
DS死神4火焰传承者
_______ the forest park is far away, a lot
火车k926一般12车厢是卧铺还是硬座
杨振宁说:“我是一个信奉佛教的科学家。
提单空白背书要怎么操作
当脑子里有两种思想进行斗争时,怎么让
邮政银行存折把钱全部取出来之后会不会销户。
百度退票网址
孙悟空的故事作文200字。
谁买AMD的优啦说说使用感受
百分之二十五,四十斤料应该兑多少玉米
一串led灯单独接火线为什么有的微亮有的不亮
推荐资讯
怎样从电脑上对Pantone色卡
牛奶是高蛋白吗
多少里面有79个千分之一
苏州到杭州富阳市怎么坐车
下列词语中加点的字,每对读音都不相同的一组
唉失败。。
南宁汉服出租哪家好些呀?
泰国机场有没有鳄鱼皮带卖
什么是产业互联?什么是产业互联网?
怎么才能查到58同城上面求职简历的联系方式
我的男神腿哥是不是牵手成功了
Q345R(R-HIC)用什么焊接,选择焊材的时候需不
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?