如何通过GETPOST方法调用HTTP接口高效获取JSON数据?
- 内容介绍
- 文章标签
- 相关推荐
本文共计241个文字,预计阅读时间需要1分钟。
plaintextgistfile1.txt: public JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject=null; try { final URL url=new URL(requestUrl); // 创建代理服务器 InetSocketAddress addr=new InetSocketAddress(127.0.0.1, 8080); }}
gistfile1.txtpublic JSONObject api.adf.ly/api.php"; String para = "key=youkeyid&youuid=uid&advert_type=int&domain=adf.ly&url=somewebsite.com"; String sr=HttpRequestUtil.sendPost(url,para,true); System.out.println(sr); } } /** * 发送Http post请求 * * @param xmlInfo * json转化成的字符串 * @param URL * 请求url * @return 返回信息 */ public static String doHttpPost(String xmlInfo, String URL) { System.out.println("发起的数据:" + xmlInfo); byte[] xmlData = xmlInfo.getBytes(); InputStream instr = null; java.io.ByteArrayOutputStream out = null; try { URL url = new URL(URL); URLConnection urlCon = url.openConnection(); urlCon.setDoOutput(true); urlCon.setDoInput(true); urlCon.setUseCaches(false); urlCon.setRequestProperty("content-Type", "application/json"); urlCon.setRequestProperty("charset", "utf-8"); urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length)); System.out.println(String.valueOf(xmlData.length)); DataOutputStream printout = new DataOutputStream( urlCon.getOutputStream()); printout.write(xmlData); printout.flush(); printout.close(); instr = urlCon.getInputStream(); byte[] bis = IOUtils.toByteArray(instr); String ResponseString = new String(bis, "UTF-8"); if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { System.out.println("返回空"); } System.out.println("返回数据为:" + ResponseString); return ResponseString; } catch (Exception e) { e.printStackTrace(); return "0"; } finally { try { out.close(); instr.close(); } catch (Exception ex) { return "0"; } } }
本文共计241个文字,预计阅读时间需要1分钟。
plaintextgistfile1.txt: public JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject=null; try { final URL url=new URL(requestUrl); // 创建代理服务器 InetSocketAddress addr=new InetSocketAddress(127.0.0.1, 8080); }}
gistfile1.txtpublic JSONObject api.adf.ly/api.php"; String para = "key=youkeyid&youuid=uid&advert_type=int&domain=adf.ly&url=somewebsite.com"; String sr=HttpRequestUtil.sendPost(url,para,true); System.out.println(sr); } } /** * 发送Http post请求 * * @param xmlInfo * json转化成的字符串 * @param URL * 请求url * @return 返回信息 */ public static String doHttpPost(String xmlInfo, String URL) { System.out.println("发起的数据:" + xmlInfo); byte[] xmlData = xmlInfo.getBytes(); InputStream instr = null; java.io.ByteArrayOutputStream out = null; try { URL url = new URL(URL); URLConnection urlCon = url.openConnection(); urlCon.setDoOutput(true); urlCon.setDoInput(true); urlCon.setUseCaches(false); urlCon.setRequestProperty("content-Type", "application/json"); urlCon.setRequestProperty("charset", "utf-8"); urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length)); System.out.println(String.valueOf(xmlData.length)); DataOutputStream printout = new DataOutputStream( urlCon.getOutputStream()); printout.write(xmlData); printout.flush(); printout.close(); instr = urlCon.getInputStream(); byte[] bis = IOUtils.toByteArray(instr); String ResponseString = new String(bis, "UTF-8"); if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { System.out.println("返回空"); } System.out.println("返回数据为:" + ResponseString); return ResponseString; } catch (Exception e) { e.printStackTrace(); return "0"; } finally { try { out.close(); instr.close(); } catch (Exception ex) { return "0"; } } }

