|
|
@@ -104,7 +104,7 @@ public class HttpXmlUtil {
|
|
|
|
|
|
lon.setUrl(url);
|
|
|
lon.setRequestData(xmlString);
|
|
|
- Response response;
|
|
|
+ Response response = null;
|
|
|
String responseString = null;
|
|
|
try {
|
|
|
|
|
|
@@ -133,6 +133,7 @@ public class HttpXmlUtil {
|
|
|
return lon;
|
|
|
} finally {
|
|
|
System.out.println("返回参数=" + responseString);
|
|
|
+ response.close();
|
|
|
log.debug("XML返回url:{},响应参数:{}", url, responseString);
|
|
|
}
|
|
|
|
|
|
@@ -162,15 +163,15 @@ public class HttpXmlUtil {
|
|
|
*
|
|
|
* @param url xml请求数据地址
|
|
|
* @param xmlString 发送的xml数据流
|
|
|
- * @return map key:requestData key:responseData
|
|
|
+ * @return
|
|
|
*/
|
|
|
public static String sendXmlPostReq(String url, String xmlString, Logger log,String textHead) {
|
|
|
|
|
|
// System.out.println("请求参数=" + xmlString);
|
|
|
if(null != log) log.debug(textHead + "XML请求url:{},\n请求参数:{}", url, xmlString);
|
|
|
|
|
|
- Response response;
|
|
|
String responseString = null;
|
|
|
+ Response response = null;
|
|
|
try {
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient.Builder()
|
|
|
@@ -193,28 +194,13 @@ public class HttpXmlUtil {
|
|
|
responseString = response.body().string();
|
|
|
}
|
|
|
|
|
|
- String[] patterns = new String[]{
|
|
|
- // This will remove empty elements that look like <ElementName/>
|
|
|
- "\\s*<\\w+/>",
|
|
|
- // This will remove empty elements that look like <ElementName></ElementName>
|
|
|
- "\\s*<\\w+></\\w+>",
|
|
|
- // This will remove empty elements that look like
|
|
|
- // <ElementName>
|
|
|
- // </ElementName>
|
|
|
- "\\s*<\\w+>\n*\\s*</\\w+>"
|
|
|
- };
|
|
|
- for (String pattern : patterns) {
|
|
|
- Matcher matcher = Pattern.compile(pattern).matcher(responseString);
|
|
|
- responseString = matcher.replaceAll("");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ log.error(textHead + "请求异常:"+e.getMessage() + ",原始返回:" + response);
|
|
|
throw new SystemException("远程接口请求失败,请联系管理员。");
|
|
|
} finally {
|
|
|
- System.out.println("返回参数=" + responseString);
|
|
|
if(null != log) log.debug(textHead + "XML返回url:{},\n响应参数:{}", url, responseString);
|
|
|
+ response.close();
|
|
|
}
|
|
|
|
|
|
return responseString;
|
|
|
@@ -224,42 +210,7 @@ public class HttpXmlUtil {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public static String sendPost2(String url, String xmlString) {
|
|
|
- System.out.println("请求参数=" + xmlString);
|
|
|
- //创建httpclient工具对象
|
|
|
- HttpClient client = new HttpClient();
|
|
|
- //创建post请求方法
|
|
|
- PostMethod myPost = new PostMethod(url);
|
|
|
- //设置请求超时时间
|
|
|
- client.setConnectionTimeout(3000 * 1000);
|
|
|
- String responseString = null;
|
|
|
- try {
|
|
|
- //设置请求头部类型
|
|
|
- myPost.setRequestHeader("Content-Type", "text/xml");
|
|
|
- myPost.setRequestHeader("charset", "utf-8");
|
|
|
- //设置请求体,即xml文本内容,一种是直接获取xml内容字符串,一种是读取xml文件以流的形式
|
|
|
- myPost.setRequestBody(xmlString);
|
|
|
- int statusCode = client.executeMethod(myPost);
|
|
|
- //只有请求成功200了,才做处理
|
|
|
- if (statusCode == HttpStatus.SC_OK) {
|
|
|
- InputStream inputStream = myPost.getResponseBodyAsStream();
|
|
|
- BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
|
|
|
- StringBuffer stringBuffer = new StringBuffer();
|
|
|
- String str = "";
|
|
|
- while ((str = br.readLine()) != null) {
|
|
|
- stringBuffer.append(str);
|
|
|
- }
|
|
|
- responseString = stringBuffer.toString();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- myPost.releaseConnection();
|
|
|
- }
|
|
|
- System.out.println("返回参数=" + responseString);
|
|
|
|
|
|
- return responseString;
|
|
|
- }
|
|
|
|
|
|
|
|
|
//报价返回
|
|
|
@@ -830,10 +781,10 @@ public class HttpXmlUtil {
|
|
|
"</packet></response></ns2:dzbdUrlResponse></S:Body></S:Envelope>";
|
|
|
|
|
|
private static String HHH = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:queryVehicleXnyQuotationServiceResponse xmlns:ns2=\"http://newWS.front.app.echannel.ebiz.alltrust.com/\"><response><?xml version='1.0' encoding='UTF-8'?>\n" +
|
|
|
- "<responseMessage>\n" +
|
|
|
- " <Applicant>\n" +
|
|
|
- " <CAppNme>乔佳飞</CAppNme>\n" +
|
|
|
- " <CAreaCde>103132</CAreaCde>\n" +
|
|
|
+ "<responseMessage>" +
|
|
|
+ " <Applicant>" +
|
|
|
+ " <CAppNme>乔佳飞</CAppNme>" +
|
|
|
+ " <CAreaCde>103132</CAreaCde>" +
|
|
|
" <CCertfCde>140121198702020677</CCertfCde>\n" +
|
|
|
" <CCertfCls>120001</CCertfCls>\n" +
|
|
|
" <CCity>140100</CCity>\n" +
|
|
|
@@ -1488,6 +1439,26 @@ public class HttpXmlUtil {
|
|
|
// String responseStr = FFF;
|
|
|
// String responseStr = GGG;
|
|
|
String responseStr = HHH;
|
|
|
+ System.err.println("responseStr:"+responseStr.replaceAll("\n",""));
|
|
|
+ String s0 = responseStr.replaceAll("\n","");
|
|
|
+// System.err.println("responseStrForm:"+s0);
|
|
|
+ String s1 = s0.replaceAll("<","<");
|
|
|
+ System.err.println("responseStrForm:"+s1.replaceAll(">",">"));
|
|
|
+
|
|
|
+ String[] patterns = new String[]{
|
|
|
+ // This will remove empty elements that look like <ElementName/>
|
|
|
+ "\\s*<\\w+/>",
|
|
|
+ // This will remove empty elements that look like <ElementName></ElementName>
|
|
|
+ "\\s*<\\w+></\\w+>",
|
|
|
+ // This will remove empty elements that look like
|
|
|
+ // <ElementName>
|
|
|
+ // </ElementName>
|
|
|
+ "\\s*<\\w+>\n*\\s*</\\w+>"
|
|
|
+ };
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ Matcher matcher = Pattern.compile(pattern).matcher(responseStr);
|
|
|
+// System.err.println("matcher:"+matcher.replaceAll(""));
|
|
|
+ }
|
|
|
|
|
|
String str = YcBuildData.cleanResponseStr(responseStr);
|
|
|
|