首页 > 代码库 > httpclient4.3 设置cookie

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