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獲取網頁資源代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.