通过json发送Http请求

本文介绍了一个使用 Java 通过 HTTP POST 请求调用 Kettle Web API 的示例,展示了如何启动一个 Kettle 作业。该示例包括了发送 JSON 格式的数据包以指定作业路径和名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


package com.jusfoun;

public class StartReq {
	
	private String path;
	private String jobname;
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getJobname() {
		return jobname;
	}
	public void setJobname(String jobname) {
		this.jobname = jobname;
	}
 

}


package com.jusfoun;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
/**
 * 发送http请求 
 * @author mengshanfeng
 * @date 20160529
 */
public class KettleHttpPost {

	private Gson gson = new Gson();//工具

	private String startUrl = "http://192.168.172.221:8080/kettleWeb/startJob";
	//private String startUrl = "http://192.168.172.221:8080/kettleWeb/stopJob";

	public void startJob(StartReq req) throws IOException {
		String json = gson.toJson(req);//转为json
		//发送请求并获取结果
		String result = doPost(startUrl, "data=" + json);
		System.out.println(result);

	}
public static void main(String[] args){
	
	KettleHttpPost a  = new KettleHttpPost();
	StartReq req = new StartReq();
	req.setPath("etl/");;
	req.setJobname("job");
	
	try {
		a.startJob(req);
	} catch (IOException e) {
 		e.printStackTrace();
	}
	
}
	
	private String doPost(String urlPath, String data) throws IOException {
		StringBuilder sub = new StringBuilder();
		System.out.println("调用kettle服务:urlPath="+urlPath+"; urlPath="+data);
		for (int i = 0; i < 3; i++) {
			// 建立连接
			try {
				URL url = new URL(urlPath);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				
				// 设置连接请求属性post
				conn.setDoOutput(true);
				conn.setDoInput(true);
				
				// 忽略缓存
				conn.setUseCaches(false);
				conn.setRequestMethod("POST");
				conn.setRequestProperty("accept", "*/*");
				conn.setRequestProperty("connection", "Keep-Alive");
				conn.setRequestProperty("user-agent",
						"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
				conn.setRequestProperty("Charset", "UTF-8");
				
				// 获取URLConnection对象对应的输出流
				PrintWriter out = new PrintWriter(conn.getOutputStream());
				out.print(data);
				out.close();
				
				// 定义BufferedReader输入流来读取URL的响应
				int code = conn.getResponseCode();
				if (HttpURLConnection.HTTP_OK == code) {
					BufferedReader in = new BufferedReader(new InputStreamReader(
							conn.getInputStream(), "UTF-8"));
					String line;
					while ((line = in.readLine()) != null) {
						sub.append(line);
					}
					in.close();
				}
				
				System.out.println("调用kettle服务成功:"+"第"+(i+1)+"次"+";urlPath="+urlPath+";data:"+data);
				
				return sub.toString();
			} catch (IOException e) {
				sub = new StringBuilder();
				System.out.println("调用kettle服务失败:"+"第"+(i+1)+"次"+";urlPath="+urlPath+";data:"+data+"Message:"+e.getMessage());
			}
		}
		
		return sub.toString();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值