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