[mb-devel] Using the Webservice Digest Authentication

Paul Taylor paul_t100 at fastmail.fm
Tue Dec 19 17:55:07 UTC 2006


Hi I am trying to to submit Puid/MusicBrainz pairs, but I am struggling 
to get the digest authentication, the Java code below fails with 
HttpCode 401 (Authorization Failure). Ive read up a few articles, but 
exactly how it works seem to vary, is there anymore info on the 
MusicBrainz usage, or can anyone see where I am going wrong with the 
implementation below ( the user and password have been changed)

thanks Paul


import java.net.HttpURLConnection;
import java.net.URL;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 */
public class TestPostPuid
{

    public static void main(String []args)  throws Exception
    {
        URL url = new URL("http://musicbrainz.org//ws/1/track/");
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setRequestMethod("POST");
        uc.setDoOutput(true);
        uc.setRequestProperty("Content-Type", 
"application/x-www-form-urlencoded");
        uc.setRequestProperty("Proxy-Authenticate", "Digest 
realm=musicbrainz.org");
        String authentication = new sun.misc.BASE64Encoder().encode( 
("testuser" + ":"+ "testpassword").getBytes() );
        uc.setRequestProperty("Proxy-Authorization", "Digest "+ 
authentication );

        PrintWriter out = new PrintWriter(uc.getOutputStream());
        
out.println("client=Testprog1.0&puid=4d8cad37-054d-4421-855e-60f111cf574a%2B2660da86-081f-eca6-12c7-739b37228326");
        out.close();
        int responseCode = uc.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK)
        {
            System.out.println("Response Code is:"+uc.getResponseCode());
        }
        else
        {
            BufferedReader in = new BufferedReader(new 
InputStreamReader(uc.getInputStream()));
            System.out.println("Response from post");
            while(in.ready())
            {
                System.out.println(in.readLine());
            }
        }
    }
}



More information about the MusicBrainz-devel mailing list