如何修复关于查询中非法字符的 URIException
Posted
技术标签:
【中文标题】如何修复关于查询中非法字符的 URIException【英文标题】:How to fix an URIException about an illegal character in query 【发布时间】:2019-11-09 05:42:03 【问题描述】:我正在使用 java.net 包从 Web 服务下载 xml 文件,但我收到此错误:“Illegal character in query at index 103: http://.............../current?path=//Controller/Components/Path/DataItems/DataItem[@type="PART_COUNT"]
我已经找好了职位。它应该是“=”。
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Builder;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class App
public static void main( String[] args )
HttpClient client = HttpClient
.newBuilder()
.version(Version.HTTP_2)
.build();
Builder builder;
try
builder = (Builder) HttpRequest.newBuilder(new URI("http://............./current?path=//Controller/Components/Path/DataItems/DataItem[@type=\"PART_COUNT\"]"));
HttpRequest request = ((java.net.http.HttpRequest.Builder) builder).GET().build();
HttpResponse httpResponse = client.send(request, BodyHandlers.ofString());
String body = (String) httpResponse.body();
System.out.println(body);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (Exception e)
System.out.println(e.getMessage());
我希望从 xml 文件中的 url 下载信息。现在我要在屏幕上打印。
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
<compilerArgument>--add-modules=jdk.incubator.httpclient</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>--add-modules=jdk.incubator.httpclient</argLine>
</configuration>
</plugin>
</plugins>
</build>
【问题讨论】:
Java equivalent to javascript's encodeURIComponent that produces identical output?的可能重复 【参考方案1】:尝试对查询参数值进行urlencode
//Controller/Components/Path/DataItems/DataItem[@type=\"PART_COUNT\"]
并传递编码值
%2F%2FController%2FComponents%2FPath%2FDataItems%2FDataItem%5B%40type%3D%5C%22PART_COUNT%5C%22%5D
https://docs.oracle.com/javase/8/docs/api/index.html?java/net/URLEncoder.html
【讨论】:
这是输出“类 jdk.internal.net.http.HttpRequestBuilderImpl 不能转换为类 java.net.http.HttpClient$Builder (jdk.internal.net.http.HttpRequestBuilderImpl 和 java. net.http.HttpClient$Builder 在加载器'platform'的模块 java.net.http 中)".......上面我把我的 pom.xml 中的代码【参考方案2】:我解决了我的问题。我使用了错误的 java 版本,因为 Java 11 中引入了 HTTP 客户端库。我用 Java 11 创建了一个新项目。下面我将为大家展示正确的代码。希望对你有用。
public class HttpTest
public static void main(String[] args)
HttpClient client = HttpClient
.newBuilder()
.version(Version.HTTP_2)
.build();
String query ="//Controller/Components/Path/DataItems/DataItem[@type=\"PART_COUNT\"]";
String encodedQuery = encodeValue(query);
System.out.println(encodedQuery);
System.out.println();
java.net.http.HttpRequest.Builder builder;
try
builder = HttpRequest.newBuilder(new URI("http://.........../current?path="+encodedQuery));
HttpRequest request = builder.GET().build();
HttpResponse httpResponse = client.send(request, BodyHandlers.ofString());
String body = (String) httpResponse.body();
System.out.println(body);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (Exception e)
System.out.println(e.getMessage());
private static String encodeValue(String value)
try
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
catch (UnsupportedEncodingException ex)
throw new RuntimeException(ex.getCause());
【讨论】:
以上是关于如何修复关于查询中非法字符的 URIException的主要内容,如果未能解决你的问题,请参考以下文章