java访问http和https的方法(+需要用户名密码 认证的网站)

1 通过url进行访问

/**
 * 使用URL类进行访问http和https
 */
public class URLTest {
	public static void main(String[] args) {
//		String https2="https://www.apiopen.top/journalismApi";
		String https="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&code=code&grant_type=authorization_code";
		InputStream in=null;
		try {
			URL url=new URL(https);
			HttpsURLConnection openConnection = (HttpsURLConnection) url.openConnection();
			String protocol = url.getProtocol();
			System.out.println(protocol);
			openConnection.connect();
  			in = openConnection.getInputStream();
  			
			StringBuilder builder=new StringBuilder();
			BufferedReader bufreader =new BufferedReader(new InputStreamReader(in));
			for (String temp=bufreader.readLine();temp!=null;temp= bufreader.readLine()) {
				builder.append(temp);
			}
			System.out.println(builder);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

访问结果:

https
{"errcode":41004,"errmsg":"appsecret missing, hints: [ req_id: ODkxIA06672030 ]"}

2 通过httpClient进行访问

/**
 * 使用HttpClient来访问https和http
 * 注意:url地址中不能有null格,不然会报错
 */
public class HttpsTest {
	public static void main(String[] args) {
		CloseableHttpClient client = HttpClients.createDefault();
		String url="https://www.apiopen.top/journalismApi";
		HttpGet get=new HttpGet(url);
		try {
			CloseableHttpResponse execute = client.execute(get);
			HttpEntity entity = execute.getEntity();
			InputStream in = entity.getContent();
			StringBuilder builder=new StringBuilder();
			BufferedReader bufreader =new BufferedReader(new InputStreamReader(in));
			for (String temp=bufreader.readLine();temp!=null;temp= bufreader.readLine()) {
				builder.append(temp);
			}
			System.out.println(builder.toString());
		} catch (ClientProtocolException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}

 

使用httpclient的pom依赖:

	    <dependency>
	 		<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.5</version>
		</dependency>

3 不删除null格会报错

4 httpClient链接需要用户名,密码认证的网站

package com.cww.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.ProtocolVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
 * 使用HttpClient来访问https和http(有的网页需要用户名和密码登录)
 * 注意:url地址中不能有null格,不然会报错
 */
public class HttpsTest {
	public static void main(String[] args) {
		CloseableHttpClient client = HttpClients.createDefault();
		String url="http://localhost:8161/admin/";
//		String url3="https://www.apiopen.top/journalismApi";
//		String url2="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&code=code&grant_type=authorization_code";
		HttpGet get=new HttpGet(url);
		ProtocolVersion protocolVersion = get.getProtocolVersion();
		System.out.println(protocolVersion.getProtocol());
		try {
			//该网页需要认证(用户名、密码)
			HttpClientContext context=new HttpClientContext();
			CredentialsProvider credentialsProvider=new BasicCredentialsProvider();
			credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
			context.setCredentialsProvider(credentialsProvider);
			CloseableHttpResponse execute = client.execute(get, context);
			//----以下一样
			HttpEntity entity = execute.getEntity();
			InputStream in = entity.getContent();
			StringBuilder builder=new StringBuilder();
			BufferedReader bufreader =new BufferedReader(new InputStreamReader(in));
			for (String temp=bufreader.readLine();temp!=null;temp= bufreader.readLine()) {
				builder.append(temp);
			}
			System.out.println(builder.toString());
		} catch (ClientProtocolException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}

 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 当一个系统使用Java进行用户登录时,通常会涉及到多个组件和步骤。在用户输入用户名密码后,如果出现404错误,则通常可以有以下几种原因造成: 1. 路由问题:404错误通常表示无法找到特定的资源或页面。可能是系统的路由配置出现问题,没有正确映射请求路径,导致服务器无法找到对应的处理器或页面。 解决办法:检查系统的路由配置,确保路径映射正确无误,可以使用合适的URL来尝试访问相关资源,确认是否能成功访问。 2. 认证问题:登录系统通常需要验证用户名密码的正确性,可能是在验证过程中出现了问题,导致无法通过认证。 解决办法:检查登录验证的代码逻辑,确保正确的用户名密码输入后可以通过认证。如果有使用数据库进行验证,也需要检查数据库连接和查询语句是否正确。 3. 错误页面配置问题:404错误也可能是由于系统没有正确配置错误页面,当发生错误时无法正确显示错误提示信息。 解决办法:检查系统的错误页面配置,确保404错误页面正确配置并且存在。 总之,处理Java系统登录时出现的404错误需要检查路由配置、认证逻辑和错误页面配置等方面,以确保用户登录过程的正常运行。以上是一些可能的原因和解决办法,具体情况需要根据实际代码和系统配置来进行分析和解决。 ### 回答2: 当在Java系统中输入用户名密码后,登录显示404错误表示所请求的资源未找到。这可能由以下几个原因引起: 1. 错误的URL路径:404错误通常是因为访问的URL路径不存在。请确保你填写的URL路径是正确的,并且系统能够找到对应的资源。 2. 缺少对应的处理程序:如果系统无法找到与请求的URL路径相关联的处理程序,它将显示404错误。检查你的代码,确保有适当的处理程序来处理用户登录请求。 3. 数据库连接错误:如果系统在验证用户名密码时无法连接到数据库,它可能会返回404错误。检查数据库连接设置和权限,并确保它们正确配置。 4. 用户名密码错误:登录过程中,如果输入的用户名密码与数据库中存储的不匹配,系统可能会显示404错误。检查用户名密码的正确性,确保输入的凭据与数据库中的一致。 5. 身份验证问题:如果系统使用身份验证机制来验证用户登录,但用户无法通过认证,系统可能会返回404错误。检查身份验证设置,确保它们正确配置,并确保用户提供的凭据满足身份验证要求。 要解决这个问题,你可以逐步检查上述可能的原因,并根据具体情况进行相应的修复。同时,寻找错误日志或调试信息可以帮助你更好地诊断和解决问题。 ### 回答3: 用户在登录时输入用户名密码后,系统返回错误代码404。404是HTTP协议中的一个错误代码,表示在服务器上请求的资源未找到。这个错误通常意味着服务器无法根据用户的请求找到所需的文件、页面或接口。 在Java中,如果在用户登录时出现404错误,可能有以下几个原因: 1. 服务器资源未找到:用户登录时,系统可能需要通过提交的用户名密码来查询数据库或其他存储介质验证用户身份。如果系统在这个过程中无法找到相应的资源(例如数据库连接失效、表不存在、接口未定义等),就会返回404错误。 2. 登录接口错误:系统的登录接口可能存在错误或问题,导致无法正确处理用户提交的用户名密码。这可能是由于代码错误、逻辑问题或其他异常引起的。修复接口问题或调试错误可以解决这个问题。 3. 用户名密码错误:用户提供的用户名密码不正确,系统在验证时无法找到相应的匹配项。应该检查用户输入的内容是否正确,并与存储的用户信息进行比对。 为了解决这个问题,您可以按照以下步骤进行: 1. 检查服务器资源:确保服务器上的资源(如数据库、表、接口)是否存在并且可用。验证数据库连接、表结构、接口定义等。 2. 调试登录接口:检查登录接口的代码,确保代码没有错误或异常。可以使用日志记录或调试器来跟踪程序执行过程,找出可能导致404错误的问题所在。 3. 验证用户名密码:检查用户输入的用户名密码是否正确,与存储的用户信息进行比对。可以使用调试输出或日志记录来查看输入和比对的结果。 通过以上步骤,您应该能够找到导致404错误的原因,并进行相应的修复措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值