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);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值