public static String CallJSHttpRequest(String data,String actionType,String invoiceAdderss){
// Properties prop = InfProResourceLoader.loadPropertyFile("");
StringBuffer urlBuf = new StringBuffer();
// String url = prop.getProperty("url");
// String action = prop.getProperty(actionType);
// String url = "http://61.144.36.122:9000/TaxHttpService/tax_";
String url = invoiceAdderss+"/TaxHttpService/tax_";
String action = actionType;
urlBuf.append(url);
urlBuf.append(action);
// if(data!=null && !"".equalsIgnoreCase(data)){
// try {
// urlBuf.append("?inputJson="+URLEncoder.encode(data, "utf-8"));
// } catch (UnsupportedEncodingException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }//
//
// }
try {
URL localURL = new URL(urlBuf.toString());
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("ContentType", "utf-8");
// httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length()));
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setConnectTimeout(100000);
DataOutputStream out = null;
try {
out = new DataOutputStream(connection.getOutputStream());
}catch (ConnectException e) {
JSONObject json = new JSONObject();
// {"RetMsg":"金税卡已经打开","RetCode":"3001"}
json.put("RetCode", "999");
json.put("RetMsg", "亲,开票服务器未响应,请检查地址是否正确!开票地址:" + invoiceAdderss);
httpURLConnection.disconnect();
// locker = false;
return json.toString();
}catch (Exception e) {
logger.info("服务器无响应 ");
JSONObject json = new JSONObject();
// {"RetMsg":"金税卡已经打开","RetCode":"3001"}
json.put("RetCode", "999");
json.put("RetMsg", "连接失败! " + e.getMessage());
httpURLConnection.disconnect();
// locker = false;
return json.toString();
}
// 发送请求
if (data != null) {
String content = "inputJson="
+ URLEncoder.encode(data, "utf-8");
logger.info("参数发送:" + data);
out.writeBytes(content);
} else {
logger.info("无参数发送.");
}
out.flush();
out.close(); // flush and close
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
// outputStream = httpURLConnection.getOutputStream();
// outputStreamWriter = new OutputStreamWriter(outputStream,"utf-8");
//
//数据通过参数传递
// outputStreamWriter.write(data.toString());
// outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream,"utf-8");
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
return resultBuffer.toString();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ProtocolException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return null;
}