0

I'm using java.net package to download an xml file from a web service, but I get this error:"Illegal character in query at index 103: http://.............../current?path=//Controller/Components/Path/DataItems/DataItem[@type="PART_COUNT"]

I've looked for the position. It should be "=".

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());
        }


    }
}

I expect to download the information from the url in a xml file. Now I'm going to print on screen.

<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>
Scripta14
  • 463
  • 2
  • 8
  • 22
  • Possible duplicate of [Java equivalent to JavaScript's encodeURIComponent that produces identical output?](https://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-outpu) – Adder Jun 27 '19 at 08:33

2 Answers2

2

try to urlencode the query param value

//Controller/Components/Path/DataItems/DataItem[@type=\"PART_COUNT\"]

and pass the encoded value

%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

Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21
  • This is tthe output "class jdk.internal.net.http.HttpRequestBuilderImpl cannot be cast to class java.net.http.HttpClient$Builder (jdk.internal.net.http.HttpRequestBuilderImpl and java.net.http.HttpClient$Builder are in module java.net.http of loader 'platform')".......Above I put the code from my pom.xml – Scripta14 Jun 27 '19 at 08:56
1

I solved my problem. I was using a wrong version of java because the HTTP client library that has been introduced in Java 11. I created a new project with Java 11. Below I will show the right code for everybody. I hope it will be usefull.

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());
        }
    }
}
Scripta14
  • 463
  • 2
  • 8
  • 22