获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 400 但可以在浏览器中正常访问
Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser
我尝试传递的参数是"Submitted_By:test2 OR Submitted_By:test"。
如果我在错误消息中复制并粘贴准确的 URL,我就可以正常访问 URL。当传递诸如 "Submitted_By:test2" 之类的参数时,它工作正常,但是当添加“ OR ”时,它会抛出错误。有什么想法吗?
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>)
它引用的第 144 行是所提供代码片段中的最后一行。
Search.getResultsBeta 方法:
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());
您应该使用 url = url.replaceAll(" ", "%20");
将空格编码为“%20”
通过对查询变量进行编码解决了问题。
更改我的代码来自:
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");
为此:
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");
我尝试传递的参数是"Submitted_By:test2 OR Submitted_By:test"。 如果我在错误消息中复制并粘贴准确的 URL,我就可以正常访问 URL。当传递诸如 "Submitted_By:test2" 之类的参数时,它工作正常,但是当添加“ OR ”时,它会抛出错误。有什么想法吗?
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>)
它引用的第 144 行是所提供代码片段中的最后一行。 Search.getResultsBeta 方法:
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());
您应该使用 url = url.replaceAll(" ", "%20");
通过对查询变量进行编码解决了问题。
更改我的代码来自:
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");
为此:
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");