可以用来检验返回给第三方的回应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();
}
}<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://blog.csdn.net/siqilou/article/details/48006651