gpt4 book ai didi

java - 如何修复有关查询中非法字符的 URIException

转载 作者:行者123 更新时间:2023-12-01 19:37:45 24 4
gpt4 key购买 nike

我正在使用 java.net 包从 Web 服务下载 xml 文件,但收到此错误:“索引 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>

最佳答案

尝试对查询参数值进行 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

关于java - 如何修复有关查询中非法字符的 URIException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com