从命令行发送请求时出现Http 404错误

时间:2014-12-24 18:55:36

标签: java batch-file httpurlconnection

我正在尝试从Restful URI下载文件,其代码如下:

public class JavaNetURLRESTFulClient {
    private final String USER_AGENT = "Mozilla/5.0";
    private Map<String,String> MimeTypes;
    private URL restServiceURL;
    private String authEncoded = null; 

    public JavaNetURLRESTFulClient (String URL) throws MalformedURLException {
        restServiceURL = new URL(URL);
        MimeTypes = new HashMap<String,String>();
        MimeTypes.put("XLS","application/vnd.ms-excel");
    }

    public void setAuthetication (String user, String password) throws UnsupportedEncodingException {
        String authString = user + ":" + password;
        authEncoded = DatatypeConverter.printBase64Binary(authString.getBytes());
    }
    public void download (String fileType, String path) throws IOException {
       HttpURLConnection httpConnection;
       System.setProperty("http.agent", "");
       httpConnection = (HttpURLConnection) restServiceURL.openConnection();
       httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
       httpConnection.setRequestProperty("User-Agent", USER_AGENT);
       httpConnection.setRequestProperty("Accept", MimeTypes.get(fileType));
       if (authEncoded!=null){
           httpConnection.setRequestProperty("Authorization", "Basic " + authEncoded);
           httpConnection.setRequestProperty("Proxy-Authorization", "Basic " + authEncoded);  
        }
        httpConnection.setRequestMethod("GET");
        if (httpConnection.getResponseCode() != 200) {
            throw new RuntimeException("HTTP GET Request Failed with Error code : "
                    + httpConnection.getResponseCode());
        }
       ...
    }
}

我通过这样做来调用这个方法:

url = args[0];
String[] schemes = {"http","https"};
UrlValidator urlValidator = new UrlValidator(schemes);
if (!urlValidator.isValid(url)) {
    System.out.println("<Url> must be a valid URI");
    return;
}
JavaNetURLRESTFulClient jrc = new JavaNetURLRESTFulClient (url);
jrc.setAuthetication ("user", "password");  
class1.download ("XLS","c:\tmp.xls")

当我从Eclipse运行此代码时,将URL参数传递给Run -> Run Configurations -> Arguments,它可以正常工作。但是,当我从Windows批处理文件(.BAT)运行此代码时,它会抛出一个Http 404错误:

java -jar download.jar "https://jira.< domain >/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+BSC+AND+issuetype+in+%28%22Big+Feature+Request%22%2C+Corrective%2C+%22Feature+Request%22%2C+%22Functional+Support%22%29+AND+created+%3E%3D+%222014-01-01+00%3A00%22+AND+created+%3C%3D+%222014-05-31+00%3A00%22+ORDER+BY+priority+DESC&tempMax=1000"
  • 我用&lt;替换了真实域名。域&gt;
  • 源文件编码为UTF-8以及.BAT文件

任何人都可以知道为什么代码可以在Eclipse内部运行而不是从命令行运行吗?

1 个答案:

答案 0 :(得分:1)

将命令行更改为:

java -jar download.jar "https://jira.< domain >/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%%3D+BSC+AND+issuetype+in+%%28%%22Big+Feature+Request%%22%%2C+Corrective%%2C+%%22Feature+Request%%22%%2C+%%22Functional+Support%%22%%29+AND+created+%%3E%%3D+%%222014-01-01+00%%3A00%%22+AND+created+%%3C%%3D+%%222014-05-31+00%%3A00%%22+ORDER+BY+priority+DESC&tempMax=1000"

您需要使用另一个%转义%以防止扩展为不存在的变量。