正如其他答案已經提到的那樣,理想情況下,您將使用理解如何解析,渲染和遞歸HTTP資源(即.html/css/js/png/gif/jpg /等)的工具。
話雖這麼說,如果你感覺特別受虐狂(我懷疑你),你可以這樣做自己......
這不是一個完美的解決方案,但如果我打算用攻擊這個鈍的工具,我會使用正則表達式(我不會進入正則表達式的細節,它已經是widely documented on the interwebs)。我的過程將是:
- HTTP GET我的基頁。
- 刪除所有與匹配的字符串定義「資源」(使用正則表達式)。
- 有選擇地爲這些資源遞送更多的字符串。
你已經提到你可以執行HTTP請求/響應(使用Sockets),所以我不會在這裏介紹。
瞧!
/**
* Regular expression to match file types - .js/.css/.png/.jpg/.gif
*/
public static final Pattern resources = Pattern.compile("([^\"'\n({}]+\\.(js|css|png|jpg|gif))",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
/**
* Pulls out "resources" from the provided text.
*/
public static Set<String> findResources(URL url, String text) {
Matcher matcher = resources.matcher(text);
Set<String> resources = new HashSet<>();
while (matcher.find()) {
String resource = matcher.group(1);
String urlStr = url.toString();
int endIndex = urlStr.lastIndexOf("/") + 1;
String parentPath = endIndex > 0 ? urlStr.substring(0, endIndex) : urlStr;
String fqResource = resource.startsWith("//") ? url.getProtocol() + ":" + resource :
resource.startsWith("http") ? resource
: resource.startsWith("/") ? getBaseUrl(url) + resource : parentPath + resource;
if (fqResource.contains("?")) {
fqResource = fqResource.substring(0, fqResource.indexOf("?"));
}
resources.add(fqResource);
}
return resources;
}
正則表達式:查找在css/js/png/gif/jpg
的方法結束阱形成的字符串:檢索從給定文本(又名「http響應」)中的所有字符串匹配,試圖建立一個完全限定的URL,並返回一組數據。
我已經上傳了full example here(帶有示例輸出)。玩的開心!
請提供基本代碼來澄清您的問題 –