httpClient学习

httpclientAPI帮助文档

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

在HTTP请求执行过程中,HttpClient将以下属性添加到执行上下文中:

  • HttpConnection 表示与目标服务器的实际连接的实例。

  • HttpHost 表示连接目标的实例。

  • HttpRoute 表示完整连接路由的实例

  • HttpRequest表示实际的HTTP请求。执行上下文中的最终HttpRequest对象总是表示消息的状态与 发送到目标服务器的状态完全相同。默认HTTP / 1.0和HTTP / 1.1使用相对请求URI。但是,如果请求是通过代理在非隧道模式下发送的,则URI将是绝对的。

  • HttpResponse 表示实际的HTTP响应。

  • java.lang.Boolean 表示表示实际请求是否被完全发送到连接目标的标志的对象。

  • RequestConfig 表示实际请求配置的对象。

  • java.util.List<URI> 表示在请求执行过程中接收到的所有重定向位置的集合的对象。



1. 创建HttpClient对象。

2. 构造Http 请求对象。

3. 执行HttpClient对象的execute方法,将Http请求对象作为该方法的参数。

4. 读取execute方法返回的HttpResponse结果并解析。

5、释放连接


1.1.7

许多应用程序需要模拟提交HTML表单的过程,例如,为了登录到Web应用程序或提交输入数据。HttpClient提供实体类 UrlEncodedFormEntity以方便进程。

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);

get方法

@RestController
@RequestMapping("/user")
public class UserController {


@RequestMapping("get")
public String doGet() {
String url = "https://oapi.dingtalk.com/gettoken";
String CorpId = " ";
String CorpSecret = "";
String str = "";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget2 = new HttpGet(url);
URI uri;
CloseableHttpResponse response;
try {
uri = new URIBuilder()
       .setScheme("https")
       .setHost("oapi.dingtalk.com")
       .setPath("/gettoken")
       .setParameter("corpid",CorpId)
       .setParameter("corpsecret",CorpSecret)
       .build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());


response = httpclient.execute(httpget);
System.out.println("response..."+response);
HttpEntity entity = response.getEntity();
System.out.println("entity..."+EntityUtils.toString(entity,"UTF-8"));
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}catch (URISyntaxException e) {
e.printStackTrace();
}
return "get方法";

}


AJAX应用的五个步骤

1.建立xmlHttpRequest对象

2.设置回调函数

3.使用OPEN方法与服务器建立连接

4.设置参数,请求方式,向服务器端发送数据

5.在回调函数中针对不同的响应状态进行处理


1.建立xmlHttpRequest对象

    if(window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
    if(xmlHttp.overrideMimeType) {
     xmlHttp.overrideMimeType("text/xml");
    }
   } else if(window.ActiveXobject) {
    var activeName = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
    for(var i = 0; i < activeName.length; i++) {
     try {
      xmlHttp = new ActiveXobject(activeName[i]);
      break;
     } catch(e) {}
    }
   }
   if(!xmlHttp) {
    alert("创建xmlhttprequest对象失败");
   } else {}

2.设置回调函数

   xmlHttp.onreadystatechange= callback;

   function callback(){}

3.使用OPEN方法与服务器建立连接

  xmlHttp.open("get","ajax?name="+ name,true)

 此步注意设置http的请求方式(post/get),如果是POST方式,注意设置请求头信息xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")

4.向服务器端发送数据

  xmlHttp.send(null);

  如果是POST方式就不为空

5.在回调函数中针对不同的响应状态进行处理

  if(xmlHttp.readyState == 4){       //判断交互是否成功

      if(xmlHttp.status == 200){         //获取服务器返回的数据         //获取纯文本数据

        var responseText =xmlHttp.responseText;

       document.getElementById("info").innerHTML = responseText;

      }

   }


暂时写到这 了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值