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();

}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章