如何用get方post方式向http接口发送数据
答案:2 悬赏:0 手机版
解决时间 2021-03-27 04:26
- 提问者网友:未信
- 2021-03-26 09:01
如何用get方post方式向http接口发送数据
最佳答案
- 五星知识达人网友:渊鱼
- 2019-07-07 05:19
//添加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
全部回答
- 1楼网友:神也偏爱
- 2020-07-14 23:07
本节摘要:本节主要分别介绍如何用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) {
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯