Using HTTP Basic-Auth with Google App Engine URLFetch service

后端 未结 6 610
眼角桃花
眼角桃花 2020-12-07 23:01

How can I specify the username and password for making Basic-Auth requests with App Engine\'s URLFetch service (in Java)?

It seems I can set HTTP headers:

         


        
6条回答
  •  时光取名叫无心
    2020-12-07 23:28

    This is a basic auth header over http:

    Authorization: Basic base64 encoded(username:password)

    eg:

    GET /private/index.html HTTP/1.0
    Host: myhost.com
    Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
    

    You will need to do this:

    URL url = new URL("http://www.example.com/comment");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization",
    "Basic "+codec.encodeBase64String(("username:password").getBytes());
    

    And to do that you will want to get a base64 codec api, like the Apache Commons Codec

提交回复
热议问题