Java HTTP AUTH?

I am trying to connect the desktop application that I am writing with the del.icio.us api @ Delicious API and simply provide them with my username and password and request the URL for the bookmark in my profile.

The problem is that I do not understand how to send my credentials when opening a connection.

How can I do it?

+3
source share
6 answers

From the site you are linking to:

All / v1 api require https requests and HTTP-Auth.

HTTP-Authis the header used in basic authentication.
In Java, you can simply put your credentials in the url:

http://user:pass@www.example.com/

URL.getUserInfo().

+2

HttpUrlConnection . HttpClient libraru (, NTLM - , , ).

0

HTTP, URL- :

http://user:password@www.website.com

UPDATE:

, :

https://user:password@api.del.icio.us/v1/posts/add&url=http://www.google.com&description=awesome
0

HttpURLConnection auth, , , Apache HTTPClient , HTTP, Basic Digest, .

HTTPClient Authentication Guide

0

:

public String reloadTomcatWebApplication(String user, String pwd, String urlWithParameters, boolean returnResponse){        
    URL url = null;
    try {

        url = new URL(urlWithParameters);
    } catch (MalformedURLException e) {
        System.out.println("MalformedUrlException: " + e.getMessage());
        e.printStackTrace();
        return "-1";
    }

    URLConnection uc = null;
    try {
        uc = url.openConnection();
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace(); 
        return "-12";
    }


    String userpass = user + ":" + pwd;
    String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());

    uc.setRequestProperty ("Authorization", basicAuth);

    InputStream is = null;
    try {
        is = uc.getInputStream();
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
        return "-13";
    }
    if(returnResponse){
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(is));
    StringBuffer response = new StringBuffer();

    String line = null;
    try {
        line = buffReader.readLine();
    } catch (IOException e) {
        e.printStackTrace();
        return "-1";
    }

    while (line != null) {
        response.append(line);
        response.append('\n');
        try {
            line = buffReader.readLine();
        } catch (IOException e) {
            System.out.println(" IOException: " + e.getMessage());
            e.printStackTrace();
            return "-14";
        }
    }

    try {
        buffReader.close();
    } catch (IOException e) {
        e.printStackTrace();
        return "-15";
    }       
    System.out.println("Response: " + response.toString());
    return response.toString();

    }               
    return "0";
}
0
source

I tried this for a delicious api and it worked like a charm

import urllib2,base64
req=urllib2.Request("https://api.del.icio.us/v1/posts/update")
username="Replace with your Username"
password="Replace with your password"
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string) 
response=urllib2.urlopen(req)
print response.read()
-2
source

Source: https://habr.com/ru/post/1722207/


All Articles