我需要在我的Web應用程序中加載外部URL的內容。嘗試使用java中的相對URL來收取外部URL的內容
我試過用HttpsUrlConnection和HttpCliente,我忘了它,但我有相對URL的問題,因爲它沒有工作。
如果我的web應用程序是http://example1.com
我嘗試收取http://external.com
含量,http://external.com
,/images/g.jpg
例如相對URL,試圖在http://example1.com/images/g.jpg
解決。
我很絕望,我尋找谷歌,但我什麼也找不到。
我很抱歉我的英語不好。
謝謝!!! :-)
PD:有我的代碼(代碼是太陽神說,大約爲絕對URL變化相對URL,但它doesn't工作...)
codigoHtml具有相對的HTML代碼鏈接,它不工作!
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, new HttpHost("host_to_redirect"));
HttpPost httpPost = new HttpPost("host_to_redirect/action.do");
httpPost.addHeader("Location", "host_to_redirect");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", "value"));
nameValuePairs.add(new BasicNameValuePair("name", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httpPost);
httpResponse.addHeader("Location", "host_to_redirect");
HttpEntity entity = httpResponse.getEntity();
System.out.println("----------------------------------------");
System.out.println(httpResponse.getStatusLine());
System.out.println("----------------------------------------");
if (entity != null) {
// System.out.println(EntityUtils.toString(entity));
response.setHeader("Location", "https://prepliberty.tirea.es:8065/pliberty");
response.addHeader("Location", "https://prepliberty.tirea.es:8065/pliberty");
String codigoHtml = EntityUtils.toString(entity);
codigoHtml = codigoHtml.replaceAll("plib_script/", "host_to_redirect/plib_script/");
codigoHtml = codigoHtml.replaceAll("plib_css/", "host_to_redirect/plib_css/");
codigoHtml = codigoHtml.replaceAll("plib_images/", "host_to_redirect/plib_images/");
response.getWriter().write(codigoHtml);
}
}
你可以在示例代碼中添加示例代碼,以瞭解你在嘗試什麼? – fglez
我編輯我的問題添加代碼,謝謝! –
向上使用格式化按鈕。代碼按鈕在文字前放置4個空格。這意味着一個代碼塊。 – helios