发送HTTP请求,调取第三方接口及返回数据的解析获取

导读:本篇文章讲解 发送HTTP请求,调取第三方接口及返回数据的解析获取,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

        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;
		 }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/111712.html

(0)
Java光头强的头像Java光头强

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!