apache http客户端java程序出错

时间:2016-07-19 09:53:38

标签: java httpclient apache-commons-httpclient

我遇到一个简单的程序问题,使用' apache http客户端(4.5.2版本)'从网址获取数据。

请在下面找到代码和错误:

public static void main(String[] args) throws Exception {
        String username = "user";
        String password = "pwd";
        String urlString = "xyz.com?a=b&c=d";

        org.apache.http.client.HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(urlString);
        org.apache.http.auth.UsernamePasswordCredentials creds = new org.apache.http.auth.UsernamePasswordCredentials(
                username, password);
        request.addHeader(new BasicScheme().authenticate(creds, request));

        HttpResponse response = client.execute(request);
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            result.append(line);
        }
    }

错误:

<Error><Code>InvalidArgument</Code><Message>Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified</Message>

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

我没有遇到上述错误,但是在将“http://”添加到给定

的开头之后,我能够运行此代码示例而没有错误
while ($row = mysql_fetch_assoc($result)) {
    file_put_contents("$server.txt", print_r($row, true));
}