java 模拟get登录_java模拟client发送get请求

package com.wjrcb.send.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URI;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

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

import org.apache.http.client.utils.URLEncodedUtils;

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

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

import org.apache.http.message.BasicNameValuePair;

import org.apache.log4j.Logger;

import com.wjrcb.send.dao.impl.AlertInfoSendDaoImpl;

import com.wjrcb.send.util.JarUtils;

public class AlertInfoSendAction {

private static Logger logger =

Logger.getLogger(AlertInfoSendAction.class);

private static  String filepath =

"wjrcbsend.properties";

public static void main(String[] args) {

Properties prop = new Properties();

InputStream

in=null;

FileOutputStream

fos;

JarUtils jarUtils = new

JarUtils(AlertInfoSendAction.class);

try {

logger.info(jarUtils.getJarPath()+File.separator+filepath);

in = new

FileInputStream(jarUtils.getJarPath()+File.separator+filepath);

prop.load(in);

} catch (FileNotFoundException e) {

logger.error(filepath+"文件未找到");

} catch (IOException e) {

logger.error(filepath+"文件读取异常");

}

String phone =

prop.getProperty("wjrcb.phone");

String sndchl =

prop.getProperty("wjrcb.sndchl");

String srcapp =

prop.getProperty("wjrcb.srcapp");

String

unitId=prop.getProperty("wjrcb.unitId");

String

get_URL=prop.getProperty("wjrcb.http");

//要发送的数据筛选条件

String

pbcips=prop.getProperty("wjrcb.pbcips");

String

wheresql=prop.getProperty("wjrcb.wheresql");

if(pbcips!=null

&&!"".equals(pbcips.trim())){

wheresql=wheresql+" and

node in

("+"'".concat(StringUtils.join(pbcips.split(","),"','")).concat("'")+")"

;

}

long

lastSendTime=Long.valueOf(prop.getProperty("wjrcb.lastsendtime"));

long

currenttime=System.currentTimeMillis();

try {

fos=new FileOutputStream(new

File(jarUtils.getJarPath()+File.separator+filepath));

prop.setProperty("wjrcb.lastsendtime",

String.valueOf(currenttime));

prop.store(fos, "save by

AlertInfoSendAction.class");

in.close();

fos.close();

} catch (FileNotFoundException e) {

logger.error(filepath+"文件未找到");

}catch (IOException e) {

logger.error(filepath+"文件保存异常");

}

AlertInfoSendDaoImpl

alertInfodao=new AlertInfoSendDaoImpl();

List>

resultList=alertInfodao.queryAlertInfo(currenttime,lastSendTime,wheresql);

List>

sendList=new ArrayList>();

List paramList=new

ArrayList();

if(resultList!=null

&& resultList.size()>0){

for(Map

reMap:resultList){

Map tempMap=new

HashMap();

tempMap.put("unit-id",

unitId);

tempMap.put("type-id",reMap.get("type_id"));

tempMap.put("type",

reMap.get("type_name"));

tempMap.put("level-id",reMap.get("level_id") );

tempMap.put("level",

reMap.get("level_name"));

tempMap.put("kind-id",reMap.get("kind_id"));

tempMap.put("kind",reMap.get("kind_name"));

tempMap.put("object","网络设备("+reMap.get("nodealias")+","+reMap.get("node")+")");

tempMap.put("description", reMap.get("summarycn"));

tempMap.put("datetime",

msToDate(reMap.get("firstoccurrence")));

paramList.add(JSONObject.fromObject(tempMap).toString());

}

for(String

text:paramList){

Map paramMap=new HashMap();

paramMap.put("phone",

phone);

paramMap.put("sndchl", sndchl);

paramMap.put("srcapp", srcapp);

paramMap.put("text", text);

sendList.add(paramMap);

}

}

for(Map

urlParam:sendList){

logger.info("开始发送。。。。。。。");

httpURLConnectionGet(get_URL, urlParam);

logger.info("发送结束。。。。。。。");

}

}

private static String httpURLConnectionGet(String

get_url,

Map urlParam) {

CloseableHttpClient httpCilent =

HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom()

.setConnectTimeout(5000) // 设置连接超时时间

.setConnectionRequestTimeout(5000) // 设置请求超时时间

.setSocketTimeout(5000).setRedirectsEnabled(true)//

默认允许自动重定向

.build();

try {

HttpGet httpGet = new HttpGet();

httpGet.setConfig(requestConfig);

List formparams = setHttpParams(urlParam);

String param = URLEncodedUtils.format(formparams,

"UTF-8");

logger.info("发送数据:" + get_url + param);

httpGet.setURI(URI.create(get_url +

param));

HttpResponse httpResponse = httpCilent.execute(httpGet);

logger.info("返回码:" +

httpResponse.getStatusLine().getStatusCode());

} catch (Exception e) {

logger.error(e);

} finally {

try {

httpCilent.close();

} catch (IOException e) {

logger.error(e);

}

}

return null;

}

private static List setHttpParams(Map paramMap) {

List formparams = new ArrayList();

Set> set = paramMap.entrySet();

for (Map.Entry entry : set) {

formparams.add(new

BasicNameValuePair(entry.getKey(), entry.getValue()));

}

return formparams;

}

private static String msToDate(String ms){

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd

HH:mm:ss");

Calendar c = Calendar.getInstance();

c.setTimeInMillis(Long.valueOf(ms));

return sdf.format(c.getTime());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值