java API接口文档方法调用
2016-06-20 15:11
435 查看
1.创建.properties文件,将请求的url写入该文件,这样更改请求路径时不用更改代码。
2.获取配置文件中的url,调用url.openConnection实现接口调用
notify.exception.url=http://ip:port/context/exception/happen notify.recover.url=http://ip:port/context/exception/recover
2.获取配置文件中的url,调用url.openConnection实现接口调用
package com.wondersgroup.cuteinfo.notify.sender; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.google.gson.Gson; import com.wondersgroup.test.monitor.persistence.bo.TaskResult; import com.wondersgroup.test.notify.NotifySender; import com.wondersgroup.test.notify.model.REST_CallResult; import com.wondersgroup.test.task.persistence.bo.TaskDefinition; public class ExceptionNotifySender implements NotifySender{ private static Log log = LogFactory.getLog(ExceptionNotifySender.class); @Override public void send(TaskDefinition task, TaskResult taskResult) { Properties p = new Properties(); try { //加载配置文件 p.load(ExceptionNotifySender.class.getResourceAsStream("/nitify.rest.properties")); //获取配置文件中的请求路径,并进行相应的设置 URL localURL = new URL(p.getProperty("notify.exception.url")); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setDoOutput(true); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); //这里是post请求 httpURLConnection.setRequestMethod("POST"); httpURLConnection.setConnectTimeout(30000); //获取参数 String taskName = task.getName(); Long taskDefId = task.getId(); Long taskResultId = taskResult.getId(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(new Date()); //传参方式,多个参数 String content = "taskName="+ taskName + "&taskDefId=" + taskDefId + "&taskResultId=" + taskResultId + "&time=" + time + "&message=123"; BufferedOutputStream outBuf = new BufferedOutputStream(connection.getOutputStream()); outBuf.write(content.getBytes()); outBuf.flush(); //获取返回值 byte[] bu = new byte[1024]; int readLength = -1; InputStream inputStream = httpURLConnection.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((readLength = inputStream.read(bu, 0, bu.length)) != -1) { out.write(bu, 0, readLength); } byte[] comfirm = out.toByteArray(); //关闭IO流 outBuf.close(); inputStream.close(); out.close(); String string = new String(comfirm, "UTF-8");//返回值 REST_CallResult CallResult = new REST_CallResult(); //使用new Gson().fromJson反序列化成类 REST_CallResult fromJson = new Gson().fromJson(string, CallResult.getClass()); //日志打印 log.info("异常发生调用返回----"+fromJson.toString()); } catch (IOException e) { e.printStackTrace(); log.error("ExceptionNotifySender--->>>send----调用失败"); } } public static void main(String[] args){ TaskDefinition taskDefinition = new TaskDefinition(); taskDefinition.setId((long) 1); taskDefinition.setName("task1"); TaskResult taskResult = new TaskResult(); taskResult.setId((long) 1); ExceptionNotifySender exceptionNotifySender = new ExceptionNotifySender(); exceptionNotifySender.send(taskDefinition,taskResult); } }
相关文章推荐
- 关于Java去连接HTTP地址的操作
- 使用URLConnection自动下载网络上的文章内容
- URL,URLConnection,HttpURLConnection
- Java 网络编程1--url 和urlconnection
- 安卓中的HTTP编程
- Java网络编程之URL编程
- android中网络操作使用总结(http)
- java之URL(URL,URLConnection)实例
- android http通信:HttpClient 和 HttpUrlConnection
- 为什么使用apache-httpclient而不是jdk-urlconnection
- JDK中的URLConnection参数详解
- URL,URLConnection,HttpURLConnection的简单应用
- java通过URLConnection连接WWW
- HTTP
- Java 使用 URLConnection 模拟 Http Get和Post 提交
- java根据url下载附件源码
- Android之使用URLConnection进行网络编程
- Java URLConnection请求的参数进行封装
- 创建和使用URL访问网络上的资源
- java网络编程