java post httpclient_java 使用httpclient发送post请求并获取数据

package com.zzz.httpClient;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URLEncoder;

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONObject;

import

org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;

import org.apache.commons.httpclient.Header;

import

org.apache.commons.httpclient.HttpClient;

import

org.apache.commons.httpclient.HttpException;

import

org.apache.commons.httpclient.HttpStatus;

import

org.apache.commons.httpclient.methods.GetMethod;

import

org.apache.commons.httpclient.params.HttpMethodParams;

import org.apache.http.HttpResponse;

import org.apache.http.StatusLine;

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

import org.apache.http.entity.StringEntity;

import

org.apache.http.impl.client.DefaultHttpClient;

import org.apache.log4j.Logger;

import com.formax.app.constant.StateConstant;

import com.formax.app.entity.DashBoard;

import com.formax.app.entity.ParamEntity;

import com.formax.app.security.DESTags;

import com.google.gson.Gson;

//import com.google.gson.Gson;

public class TagsHttpClientUtil {

public static Logger logger =

Logger.getLogger(TagsHttpClientUtil.class);

public static void main(String arg[]) throws Exception {

String ip_addr = "localhost";

String port = "8080";

getDashBoardsJson("http://" + ip_addr + ":" + port +

"/v1/get_dashboard_post"); //发送post请求到controller里面,对这些id进行遍历

// Thread.sleep(sleepTimes);

}

public static void sendFcTagsJson(String url) throws Exception

{

ParamEntity push = new ParamEntity();

Gson gson = new Gson();

DESTags des = new DESTags();

String s = "uid=2000074&d=" +

(System.currentTimeMillis()/1000l);

s = des.encrypt(s, StateConstant.DES_PWD_TAGS);

//当使用http请求时,此参数可以不加密

//s =  URLEncoder.encode(s,

"UTF-8");

push.setS(s);

logger.info("the  param s is : " + s );

String obj = gson.toJson(push);

logger.info("the json obj is :  "+ obj);

doPost(url, obj);

}

public static void

getDashBoardsJson(String url) throws

InterruptedException{

List lists = new ArrayList();

DashBoard ds = new DashBoard();

for (int i = 132350; i <

132365 ; i ++){

ds = new DashBoard();

ds.setId(i);

logger.info("the if of the ds is : " +

i);

lists.add(ds);

}

logger.info("the length of the lists is : " +

lists.size());

Gson gson = new Gson();

logger.info( "the  begin is

: "  + " and the trajectory xxxxxxxxxxxxx record

is : 条记录 !!");

String obj = gson.toJson(lists);

logger.info("the obj is :  "

+ obj);

doPost(url, obj);

}

public static void

sendFxTagsJson(String url) throws InterruptedException{

ParamEntity push = new ParamEntity();

push.setS("jsonpushdata");

Gson gson = new Gson();

logger.info( "the  begin is

: "  + " and the trajectory xxxxxxxxxxxxx record

is : 条记录 !!");

String obj = gson.toJson(push);

doPost(url, obj);

}

private static JSONObject doPost(String

url, String jsString) {

DefaultHttpClient client =

new DefaultHttpClient();

HttpPost post = new

HttpPost(url);

JSONObject response =

null;

BufferedReader in =

null;

try {

StringEntity s = new StringEntity(jsString);

s.setContentEncoding("utf-8");

s.setContentType("application/json");//发送json数据需要设置contentType

post.setEntity(s);

HttpResponse res = client.execute(post);

StatusLine line = res.getStatusLine();

int

status = line.getStatusCode();

System.out.println(status);

System.out.println(res);

//

读取响应输入流,获取响应数据

in =

new BufferedReader(new

InputStreamReader(res.getEntity().getContent()));

String

returnLine = "";

String

result = "";

while((result = in.readLine()) != null){

returnLine += result;

}

System.out.println("result is : " + returnLine);

in.close();

} catch (Exception e)

{

throw

new RuntimeException(e);

}

return response;

}

public static String doGet(String url,

String charset)

throws Exception {

HttpClient

httpClient = new HttpClient();

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

GetMethod getMethod

= new GetMethod(url);

getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,

5000);

getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new

DefaultHttpMethodRetryHandler());

String response =

"";

try {

int statusCode = httpClient.executeMethod(getMethod);

if (statusCode != HttpStatus.SC_OK) {

System.err.println("请求出错: "+

getMethod.getStatusLine());

}

Header[] headers = getMethod.getResponseHeaders();

for (Header h : headers)

System.out.println(h.getName() + "------------ "

+ h.getValue());

byte[] responseBody = getMethod.getResponseBody();//

读取为字节数组

response = new String(responseBody, charset);

System.out.println("----------

response:" + response);

} catch

(HttpException e) {

System.out.println("请检查输入的URL!");

e.printStackTrace();

} catch (IOException

e) {

System.out.println("发生网络异常!");

e.printStackTrace();

} finally

{

getMethod.releaseConnection();

}

return

response;

}

public static JSONObject doPost(String

url,JSONObject json){

DefaultHttpClient

client = new DefaultHttpClient();

HttpPost post = new

HttpPost(url);

JSONObject response

= null;

try {

StringEntity s = new StringEntity(json.toString());

s.setContentEncoding("utf-8");

s.setContentType("application/json");//发送json数据需要设置contentType

post.setEntity(s);

HttpResponse res = client.execute(post);

StatusLine line = res.getStatusLine();

int status = line.getStatusCode();

System.out.println(status);

System.out.println(res);

} catch (Exception

e) {

throw new RuntimeException(e);

}

return

response;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值