Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中访问https站点_Java_Https_Httpurlconnection - Fatal编程技术网

如何在java中访问https站点

如何在java中访问https站点,java,https,httpurlconnection,Java,Https,Httpurlconnection,我必须通过java代码访问https站点。但它返回401响应。我在下面包含了我的代码 try { URL u = new URL(url); HttpURLConnection http = (HttpURLConnection)u.openConnection(); http.setAllowUserInteraction(true); http.connect(); String userpassword = "HP:M0lveau"; byte[] encod

我必须通过java代码访问https站点。但它返回401响应。我在下面包含了我的代码

try {        
 URL u = new URL(url);
 HttpURLConnection http = (HttpURLConnection)u.openConnection();
 http.setAllowUserInteraction(true);
 http.connect();

 String userpassword = "HP:M0lveau";
 byte[] encoded = Base64.encodeBase64(userpassword.getBytes());
 String encodedAuthorization = new String(encoded);

 http.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
 InputStream is = http.getInputStream();

 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 StringBuilder stringBuilder = new StringBuilder();
 String line = null;
 while ((line = reader.readLine()) != null) {
   stringBuilder.append(line + "\n");
 }
 return stringBuilder.toString();

} catch (IOException ioe) {
 logger.debug("fetchDataFromServer:IOException");
 return null;
}

请尽早帮助我。。提前感谢….

401响应意味着请求需要用户身份验证。看

有点像

public static String userNamePasswordBase64(String username, String password)
{
    return "Basic " + base64Encode (username + ":" + password);
}

static public String base64Encode(String s)
{
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    Base64OutputStream out = new Base64OutputStream(bout);
    try
    {
        out.write(s.getBytes());
        out.flush();
    } catch (IOException e)
    {
    }
return bout.toString();
}
然后使用

http.setRequestProperty ("Authorization",userNamePasswordBase64("HP","M01veau"));
http.connect();
还要手动检查这些凭据在给定url上是否正常工作。
另外

尝试在调用connect()之前设置授权标头。

我得到了它。。这是我的密码

private static String fetchDataFromServer() throws HttpException, IOException, NoSuchAlgorithmException, KeyManagementException {

        logger.trace("__ENTERING CluemasterData::fetchDataFromServer()");

        try {
            URL u = new URL("https://....");
            HttpsURLConnection http = (HttpsURLConnection)u.openConnection();
            Authenticator.setDefault( new MyAuthenticator() );
            http.setAllowUserInteraction(true);
            http.setRequestMethod("GET");
            http.connect();

            InputStream is = http.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line + "\n");
            }
            return stringBuilder.toString();    
        }
        catch (HttpException he) {

            return null;
        } 
        catch (IOException ioe) {

            return null;
        }

    }
类MyAuthenticator

import java.net.Authenticator;
import java.net.PasswordAuthentication;

class MyAuthenticator extends Authenticator
   {
   /**
   * Called when password authorization is needed.
   * @return The PasswordAuthentication collected from the
   * user, or null if none is provided.
   */
   protected PasswordAuthentication getPasswordAuthentication()
      {
      return new PasswordAuthentication ( "username", "password".toCharArray() );
      }
   }

我想你的意思是设置用户名和密码。但是我已经设置好了。@Sarika:当你正常通过浏览器访问这个网站时,你的凭证(usename/passwd)有效吗??url是什么?@Sarika:请参见编辑后的答案中的新函数base64Encode()。也许会有帮助。也可以看到@sARIKA:Maurice提到的“尝试在调用connect()之前设置授权头”,就像我在回答中提到的那样。该包是包gnu.inet.mime.base64。检查这个-还有这个@Sarika:你所说的“对主机站点的响应”是什么意思?你得到了怎样的回应?假设您的用户名和密码正确,我上面给出的身份验证代码是正确的。而且非常重要-当您正常通过浏览器访问此URL时,您的用户名/密码工作正常吗??