码迷,mamicode.com
首页 > Web开发 > 详细

http操作,模拟第三方接口回调通知

时间:2015-08-26 17:53:48      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:http   第三方接口回调   notify   


可以用来检验返回给第三方的回应SUCCESS是否正确,包括编码,是否有空格


模拟第三方接口发送回调notify

<span style="white-space:pre">	</span>import java.io.IOException;
	import java.io.InputStream;
	import java.io.OutputStream;
	import java.net.HttpURLConnection;
	import java.net.URL;
	
	private static byte[] readContent(final InputStream in, int length) throws IOException {
		byte dataBytes[] = new byte[length];		
		int bytesRead = 0;
		int n = 0;
		int leftbytes = length;
		while (leftbytes > 0
				&& (n = in.read(dataBytes, bytesRead, leftbytes)) != -1) {
			leftbytes = length - bytesRead;
			bytesRead = bytesRead + n;
		}
		return dataBytes;
	}

	public static void main(String args[]){
		//double a = 1/3.0;
		//System.out.println("你丫的是啥"+a);
		try {
			
			URL url = new URL("https://www.1hedai.com/page/style4/yeepayCallBack/SpaceNotify.jsp");
			HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
			urlcon.setReadTimeout(5000);
			urlcon.setDoOutput(true);
			urlcon.setDoInput(true);
			urlcon.setRequestMethod("POST");
			urlcon.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			OutputStream out = urlcon.getOutputStream();
			
			out.write("测试返回的SUCCESS".getBytes("UTF-8"));
			out.flush();
			out.close();
			
			int length = urlcon.getContentLength();
			InputStream in = urlcon.getInputStream();
			byte[] b = readContent(in, length);
			String result =  new String(b, "UTF-8");
			System.out.println("收到的回调:" + result + "0");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



服务器接收到notify,并发送回应SUCCESS

<span style="white-space:pre">	</span>//接口回调
	//打印参数
	System.out.println("+++++++++++++++ space Notify ++++++++++++++++++");
	
	//声明JSP对象
	try{
		// 验签
		//boolean flag = SignUtil.verifySign(sourceMessage, signMsg);
		//out.print("SUCCESS");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		//response.getWriter().write("SUCCESS");
	
		OutputStream outStream = response.getOutputStream();
		outStream.write("SUCCESS".getBytes("UTF-8"));
		//outStream.flush();
		outStream.close(); 
	}catch(Exception e){
		 e.printStackTrace();
	}


版权声明:本文为博主原创文章,未经博主允许不得转载。

http操作,模拟第三方接口回调通知

标签:http   第三方接口回调   notify   

原文地址:http://blog.csdn.net/siqilou/article/details/48006651

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!