/**
* @Title: getResult
* @Description: 回傳結果
* @param @param data
* @param @return
* @param @throws Exception 設定檔案
* @return Map<String,Object> 回傳型別
* @throws
*/
@SuppressWarnings("deprecation")
private String getResponse(String reqXml, String transCode, String packageId) throws Exception {
String proxyIp = Property.getProperty("ICBC.QPAY.PROXY.IP");
String proxyPort = Property.getProperty("ICBC.QPAY.PROXY.PORT");
// 3.與支付平臺進行通訊
String singData = HttpRequest.sendPost(proxyIp, proxyPort, Property.getProperty("ICBC.QPAY.SIGNSERVER"), reqXml, "INFOSEC_SIGN/1.0", true, "GBK");
singData = singData.substring(singData.indexOf("<sign>") + 6, singData.indexOf("</sign>"));
// 發送交易請求
String param = "Version=0.0.1.0&TransCode=" + transCode + "&BankCode=" + BANKCODE + "&GroupCIS=" + GIS
+ "&ID=" + ID +"&PackageID=" + packageId + "&Cert=&reqData="https://bbs.csdn.net/topics/ + URLEncoder.encode(singData);
String resData = HttpRequest.sendPost(proxyIp, proxyPort, Property.getProperty("ICBC.QPAY.HTTPSERVER"), param, "application/x-www-form-urlencoded", true, "GBK");
//解密后的結果
String result = new String(Base64.decode(resData.split("reqData="https://bbs.csdn.net/topics/)[1]),"GBK");
log.debug("回傳報文: {}", result);
return result;
}
/**
* @Title: sendPost
* @Description: TODO(這里用一句話描述這個方法的作用)
* @param @param url 請求地址
* @param @param param 請求引數
* @param @param contentType
* @param @param useProxy 是否使用代理
* @param @return 設定檔案
* @return String 回傳型別
* @throws
*/
public static String sendPost(String proxyIp, String proxyPort, String url, String param, String contentType, boolean useProxy, String charset) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = null;
if(useProxy) {
/*構造Proxy物件,以適用于代理上網的方式*/
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(proxyIp), Integer.parseInt(proxyPort));
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
conn = realUrl.openConnection(proxy);
} else {
conn = realUrl.openConnection();
}
// 設定通用的請求屬性
conn.setRequestProperty("Content-Type", contentType);
//conn.setRequestProperty("Content-Length", contentLength);
//conn.setRequestProperty("accept", "*/*");
//conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Jakarta Commons-HttpClient/3.1");
// 發送POST請求必須設定如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection物件對應的輸出流
if (StringUtils.isEmpty(charset)) {
out = new PrintWriter(conn.getOutputStream());
} else {
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), charset));
}
// 發送請求引數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的回應
//in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
if (StringUtils.isEmpty(charset)) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
}
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new TtyException("發送POST請求出現例外。", e);
}
// 使用finally塊來關閉輸出流、輸入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
throw new TtyException("關閉流失敗。");
}
}
return result;
}
以上是讀取輸入流并顯示的代碼,通過BUFFERREADER的READLNE實作,但每次讀取到的回傳報文都只能讀取到一半,后面的就丟失了,下面是我個wireshark抓包的通訊記錄,在最后服務器會發出RST斷開,而之前的兩次PUS都沒有得到正確的客戶端應答ACK,請大神幫忙分析下為什么服務器會主動發送RST
uj5u.com熱心網友回復:
從時間差上來看,并不是客戶端不應答服務器,而是還沒來的及應答就被服務器RST掉了,應該是服務器發現了某種不對勁就RST了。uj5u.com熱心網友回復:
這么說有可能是服務器的問題?對方的技術人員說網路流最好用位元組陣列來讀取,不要用readline的字符來讀取,否則容易報錯,但我改了位元組來讀取貌似也沒變化uj5u.com熱心網友回復:
自己頂一個 還沒找到問題 很煩啊轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/131379.html
標籤:網絡協議與配置
下一篇:路由器的小問題,資料包如何轉發?
