开发者

Problem with downloading file from server that uses basic http authentication from my java code

开发者 https://www.devze.com 2023-02-15 19:44 出处:网络
I have written the following java code to download a file from a server that uses http basic authentication. But im getting Http 401 error.I can however download the file by hitting the url directly f

I have written the following java code to download a file from a server that uses http basic authentication. But im getting Http 401 error.I can however download the file by hitting the url directly from the browser.

    OutputStream out = null;
    InputStream in = null;
    URLConnection conn = null;

    try {
                    // Get the URL
                        URL url = new URL("http://username:password@somehost/protected-area/somefile.doc");
                    // Open an output stream for the destination file locally
                    out = new BufferedOutpu开发者_C百科tStream(new FileOutputStream("file.doc"));
                    conn = url.openConnection();
                    in = conn.getInputStream();

                    // Get the data
                    byte[] buffer = new byte[1024];
                    int numRead;
                    while ((numRead = in.read(buffer)) != -1) {
                        out.write(buffer, 0, numRead);
                    }            

     } catch (Exception exception) {
                    exception.printStackTrace();
     } 

But,im getting the following exception when i run the program :

java.io.IOException: Server returned HTTP response code: 401 for URL: http://username:password@somehost/protected-area/somefile.doc
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
        at TestDownload.main(TestDownload.java:17)

I am however able to download the file by hitting the url , http://username:password@somehost/protected-area/somefile.doc, directly from the browser.

What could be causing this problem, and any way to fix it ?

Please Help Thank You.


I'm using org.apache.http:

private StringBuffer readFromServer(String url) {
    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

            if (authState.getAuthScheme() == null) {
                Credentials credentials = new UsernamePasswordCredentials(
                        Constants.SERVER_USERNAME,
                        Constants.SERVER_PASSWORD);

                authState.setAuthScheme(new BasicScheme());
                authState.setAuthScope(AuthScope.ANY);
                authState.setCredentials(credentials);
            }
        }    
    };

    httpclient.addRequestInterceptor(preemptiveAuth, 0);

    HttpGet httpget = new HttpGet(url);
    HttpResponse response;

    InputStream instream = null;
    StringBuffer result = new StringBuffer();

    try {
        response = httpclient.execute(httpget);

etc...

0

精彩评论

暂无评论...
验证码 换一张
取 消