httpclient4.3 设置cookie

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient4.3 设置cookie相关的知识,希望对你有一定的参考价值。

package com.test;


import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


import org.apache.commons.codec.digest.DigestUtils;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.StatusLine;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicHeader;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;


import com.alibaba.fastjson.JSONObject;


public class SmsHttp {

public static final String key = "iflytek20160ebFOFYY";

public static final String url = "http://www.linyi.net/App.ResourceCloud/index.php?app=smsmas&mod=Homepage&act=sendSmsLinyiBase";

public static void main(String[] args) throws ClientProtocolException, IOException {

String mobile = "15872004131";

String content = "模板语言下雨";//模板有两个参数需要用空格隔开

String mac = DigestUtils.md5Hex(mobile+content+key).toUpperCase();//验签

sendSms(mobile, content, mac);

}

public static void sendSms(String mobile, String content,String mac) throws ClientProtocolException, IOException{

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost("http://localhost:8080/demo1/NewFile.jsp");

httpPost.addHeader(new BasicHeader("Cookie","JSESSIONID=B6FF25530B16AB46CA77B08129FECFB3"));

List<NameValuePair> formPair = new ArrayList<NameValuePair>();

formPair.add(new BasicNameValuePair("mobile", mobile));

formPair.add(new BasicNameValuePair("content", content));

formPair.add(new BasicNameValuePair("mac", mac));

UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(formPair, "UTF-8");

httpPost.setEntity(urlEntity);

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity entity = response.getEntity(); 

StatusLine status = response.getStatusLine();

System.out.println("返回的状态码为:" + status);

String ret = EntityUtils.toString(entity);

System.out.println("返回的报文为:" + ret);

EntityUtils.consume(entity); 

response.close();

httpPost.releaseConnection();

httpClient.close();

}

}


以上是关于httpclient4.3 设置cookie的主要内容,如果未能解决你的问题,请参考以下文章

HttpClient4.3.6 实现https访问

HttpClient4.3.6版本以上的http和https请求写法

HttpClient4.3教程 第二章 连接管理

java httpclient4.3连接池怎么解决大量close_wait的问题

HttpClient

请教httpclient访问https,我的步骤错哪里了