package com.example.dangdangweb.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Getjson { public static String getjson(String bookid, String platnum) { //测试 String s = "http://localhost:8080/sc/getprice?book_id="; s += bookid + "&platNum=" + platnum; try { URL url = new URL(s); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (200 == conn.getResponseCode()) {//如果资源存在 System.err.print("链接成功"); InputStream is = conn.getInputStream();//得到网络的输入流 InputStreamReader isr = new InputStreamReader(is, "utf-8");//编码格式 BufferedReader br = new BufferedReader(isr);//存入Buffer缓冲区 String str = null; StringBuffer buffer = new StringBuffer(""); while ((str = br.readLine()) != null) { buffer.append(str); } br.close(); isr.close(); is.close(); String res = null; res = buffer.toString(); return res; } } catch (IOException e) { e.printStackTrace(); } return null; } }
Java获取网页资源代码
最新推荐文章于 2021-02-20 07:46:03 发布