在java中尝试使用相对url对外部url的内容收费
我需要在我的web应用程序中加载外部url的内容 我用HttpsUrlConnection和HttpCliente试过了,我忘记了,但是相对URL有问题,因为它不起作用 如果我的webapp是在java中尝试使用相对url对外部url的内容收费,java,http,url,Java,Http,Url,我需要在我的web应用程序中加载外部url的内容 我用HttpsUrlConnection和HttpCliente试过了,我忘记了,但是相对URL有问题,因为它不起作用 如果我的webapp是http://example1.com我尝试对http://external.com,的相对urlhttp://external.com,/images/g.jpg正在尝试解决http://example1.com/images/g.jpg 我绝望了,我在找谷歌,但什么也没找到 我很抱歉我的英语不好 谢谢!
http://example1.com
我尝试对http://external.com
,的相对urlhttp://external.com
,/images/g.jpg
正在尝试解决http://example1.com/images/g.jpg
我绝望了,我在找谷歌,但什么也没找到
我很抱歉我的英语不好
谢谢!!!:-)
p.D:这是我的代码(在代码中,helios提到将相对url更改为绝对url,但它不起作用…)
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);
}
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
DefaultHttpClient httpclient=新的DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST,新的HttpHost(“HOST_to_重定向”);
HttpPost-HttpPost=newhttppost(“host_to_redirect/action.do”);
addHeader(“位置”,“主机到主机重定向”);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“名称”、“值”));
添加(新的BasicNameValuePair(“名称”、“值”));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse HttpResponse=httpclient.execute(httpPost);
httpResponse.addHeader(“位置”,“主机到主机重定向”);
HttpEntity entity=httpResponse.getEntity();
System.out.println(“--------------------------------------------------------”;
System.out.println(httpResponse.getStatusLine());
System.out.println(“--------------------------------------------------------”;
如果(实体!=null){
//System.out.println(EntityUtils.toString(entity));
response.setHeader(“位置”https://prepliberty.tirea.es:8065/pliberty");
response.addHeader(“位置”https://prepliberty.tirea.es:8065/pliberty");
字符串codigoHtml=EntityUtils.toString(实体);
codigoHtml=codigoHtml.replaceAll(“plib\u脚本/”,“主机到\u重定向/plib\u脚本/”;
codigoHtml=codigoHtml.replaceAll(“plib_css/”,“主机到主机重定向/plib_css/”;
codigoHtml=codigoHtml.replaceAll(“plib_images/”,“host_to_redirect/plib_images/”;
response.getWriter().write(codigoHtml);
}
}
您尝试的操作与Apache的类似
它基本上必须重写提供的URL。没有什么灵丹妙药。因此,如果内容不是很复杂,我应该建议将内容作为字符串抓取,并进行替换(或多次替换)
比如:
String html = ...content from URL... //beware of encoding!!! a lot of programmers neglect this!
html = html.replace(OLD_PREFIX, NEW_PREFIX);
// now you can use html
旧前缀可以是http://external.com/
和新前缀可以是http://example1.com/
您可以考虑URL前面总是有一个双引号
“
,因此前缀可以包括开头的”
。当然可能会有误译…而且你的英语也没那么差。至少写下这个问题:)谢谢你的回答Helios!!我尝试过(更改url),但仍然不起作用,当我看到源代码时,url很好,但不起作用,我想可能我必须在发送请求之前更改一些标题,我尝试查找,但我什么也找不到!!P.D:谢谢你的英语:-)当你(作为浏览器客户端)收到页面时。。。写得好吗?我的意思是:它是否包含…
?你能在问题中添加示例代码以了解你在尝试什么吗?我编辑我的问题g添加代码,谢谢!!在这里使用格式化按钮。“代码”按钮在文本前放置4个空格。这意味着一个代码块。一件事:是原始链接,还是相对链接?(如果链接是绝对链接,那么您的结果将是oldhost.com/newhost.com/dir
:)如果是,您需要转换它们吗?经验法则是尝试在main
方法中替换字符串,这是一种非正式测试。硬编码从外部服务器接收的文本,并进行所需的替换。输出结果。只有当它工作时,才可以尝试真正的应用程序。