Java Server 返回 HTTP 响应代码:401

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22677930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:14:09  来源:igfitidea点击:

Java Server returned HTTP response code: 401

javahttpapache-commons

提问by AntiGMO

Hi I write java program to do a http post request by Http Basic Authentication but it always shows error 401. My username and password is right can login the website. I don't know where wrong?

嗨,我编写了 Java 程序来通过 Http Basic Authentication 执行 http post 请求,但它总是显示错误 401。我的用户名和密码是正确的,可以登录该网站。不知道哪里错了?

 import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import sun.misc.*;

 import javax.net.ssl.HttpsURLConnection;
 import org.apache.commons.codec.*;

 @SuppressWarnings("unused")
 public class hello {

/**
 * @param args
 */
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    hello http = new hello();


    //System.out.println("Testing 1 - Send Http GET request");
    //http.sendGet();

    System.out.println("\nTesting 2 - Send Http POST request");
    http.sendPost();
}

@SuppressWarnings("unused")
private void sendPost() throws Exception {

    String url = "https://mds.datacite.org/doi";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    String userPassword= "username:password";
    String encoding = new String(org.apache.commons.codec.binary.Base64.encodeBase64(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(userPassword)));
    System.out.println(encoding);


    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "text/plain");
    con.setRequestProperty("charset", "UTF-8");
    con.setRequestProperty("Authorization","Basic"+encoding);


    String urlParameters = "doi=xxxxxx&url=http://xxxxx/dataset/1xxx099";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());

}

采纳答案by stringy05

401 == Unauthorized, which means your username/password combo are incorrect

401 == Unauthorized,这意味着您的用户名/密码组合不正确

you need a space after "Basic" in the Authorization header

您需要在授权标题中的“基本”之后留一个空格