HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范。HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口是动态分配的。当我们没有指定端口访问时,浏览器会默认帮我们添加80端口。我们也可以自己指定访问端口如:http://www.ip138.com:80。 需要注意的是,现在大多数访问都使用了HTTPS协议,而HTTPS的默认端口为443,如果使用80端口访问HTTPS协议的服务器可能会被拒绝。
发送HTTP请求,调取第三方接口:
public static boolean sendUamPost(String api, String param,String type ,String entityName) throws BusinessException, IOException {
String url1 = SysInitQuery.getParaString("GLOBLE00000000000000", "QLUamUrl")+api;
Writesendlog log = new Writesendlog();
URL url = new URL(url1);
// 打开和URL之间的连接
URLConnection uc = url.openConnection();
// 发送POST请求必须设置
uc.setDoOutput(true);
uc.setUseCaches(false);
// 设置编码格式
uc.setRequestProperty("Content-Type","application/json");
uc.setRequestProperty("Charset", "UTF-8");
uc.setRequestProperty("Content-Length", "10000");
HttpURLConnection hc = (HttpURLConnection) uc;
hc.setRequestMethod(type);
hc.setRequestProperty("Charsert", "UTF-8");
OutputStream os = null;
DataOutputStream dos = null;
String result = "";
try {
os = hc.getOutputStream();
dos = new DataOutputStream(os);
dos.write(param.getBytes());
dos.flush();
BufferedReader bReader = new BufferedReader(new InputStreamReader(hc.getInputStream(),"UTF-8"));
int ch;
while ((ch = bReader.read()) != -1) {
result += String.valueOf((char) ch);
}
}catch(Exception e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
StringBuffer buffer = stringWriter.getBuffer();
String replace = buffer.toString().replace("\n", "").replace("\t", "");
nc.bs.logging.Logger.error("统一认证部门推送error:" + replace);
log.writesendlog(replace, "UAMdeptError","");
ExceptionUtils.wrappBusinessException(entityName+":"+ e.getMessage());
} finally {
//使用finally块来关闭输出流、输入流
if (dos != null) {
try {
dos.close();
} catch (Exception e2) {
Logger.error(e2.getMessage(), e2);
}
}
if (os != null) {
try {
os.close();
} catch (Exception e2) {
Logger.error(e2.getMessage(), e2);
}
}
if(!"".equals(result)&&result!=null) {
JSONObject parseObject = JSONObject.parseObject(result);
Object object = parseObject.get("code");
if(!"".equals(object)&&object!=null) {
int parseInt = Integer.parseInt(object.toString());
if(parseInt == 0) {
log.writesendlog(param, entityName+"chenggong","");
return true;
}
}
// log.writesendlog(result, "UAMdeptfanhuizhi","");
log.writesendlog(url1+";;type:"+type+";;;数据:"+param, entityName+"error","");
}else {
// log.writesendlog(result, "UAMdeptfanhuizhi","");
log.writesendlog(url1+";;type:"+type+";;;数据:"+param, entityName+"error","");
}
log.writesendlog(url1+";;type:"+type+";;;数据:"+param+";;;返回值:"+result, "UAM","");
nc.bs.logging.Logger.error("统一认证返回值:" + result);
}
return false;
}