Search code examples
javahttp-status-code-400

Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser


The parameter I tried passing was "Submitted_By:test2 OR Submitted_By:test". If I copy and paste the exact URL in the error message I can access the URL just fine. When passing a parameter such as "Submitted_By:test2" it works fine but when adding the " OR " it throws the error. Any ideas?

12:51:53,246 ERROR [stderr] (default task-1) java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:1234/solr/STINGRA/select/?q=Submitted_By:test2 OR Submitted_By:test&fl=Submission_ID,Submitted_By,File_Name,TOT&version=2.2&start=0&rows=50&indent=on&wt=xml&callback=?&json.wrf=on_data
12:51:53,247 ERROR [stderr] (default task-1)    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
12:51:53,247 ERROR [stderr] (default task-1)    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
12:51:53,247 ERROR [stderr] (default task-1)    at java.net.URL.openStream(URL.java:1038)
12:51:53,247 ERROR [stderr] (default task-1)    at webapp.Search.getResultsBeta(Search.java:144)
12:51:53,247 ERROR [stderr] (default task-1)    at webapp.WebappController.searchResultsBeta(WebappController.java:87)
12:51:53,248 ERROR [stderr] (default task-1)    at webapp.WebappController$$FastClassBySpringCGLIB$$b0db6f6a.invoke(<generated>)

The line 144 it refers to is the last line in the provided snippet of code. The Search.getResultsBeta method:

public static Integer[] getResultsBeta(String query) {
    String inputLine;
    ArrayList<Integer> subId = new ArrayList<>();
    int numOfResults = 0;
    try {
        URL temp;
        temp = new URL("http://localhost:1234/solr/STINGRA/select/?q=" + query + "&fl=Submission_ID,Submitted_By,File_Name,TOT&version=2.2&start=0&rows=50&indent=on&wt=xml&callback=?&json.wrf=on_data");
        BufferedReader in;
        InputStreamReader sr = new InputStreamReader(temp.openStream());

Solution

  • Solved the problem by encoding the query variable.

    Changing my code from:

    URL temp;
    temp = new URL("http://localhost:1234/solr/STINGRA/select/?q=" + query + "&fl=Submission_ID,Submitted_By,File_Name,TOT&version=2.2&start=0&rows=50&indent=on&wt=xml&callback=?&json.wrf=on_data");
    

    To this:

    URL temp;
    query = URLEncoder.encode(query);
    temp = new URL("http://localhost:1234/solr/STINGRA/select/?q=" + query + "&fl=Submission_ID,Submitted_By,File_Name,TOT&version=2.2&start=0&rows=50&indent=on&wt=xml&callback=?&json.wrf=on_data");