欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 发送http和https请求的实例

程序员文章站 2023-12-16 08:50:40
http请求: 如果需要json格式的自己转下,度娘上n种姿势… //处理http请求 requesturl为请求地址 requestmethod请求方式,值为...

http请求:

如果需要json格式的自己转下,度娘上n种姿势…

//处理http请求 requesturl为请求地址 requestmethod请求方式,值为"get"或"post" 
  public static string httprequest(string requesturl,string requestmethod,string outputstr){ 
    stringbuffer buffer=null; 
    try{ 
    url url=new url(requesturl); 
    httpurlconnection conn=(httpurlconnection)url.openconnection(); 
    conn.setdooutput(true); 
    conn.setdoinput(true); 
    conn.setrequestmethod(requestmethod); 
    conn.connect(); 
    //往服务器端写内容 也就是发起http请求需要带的参数 
    if(null!=outputstr){ 
      outputstream os=conn.getoutputstream(); 
      os.write(outputstr.getbytes("utf-8")); 
      os.close(); 
    } 
    //读取服务器端返回的内容 
    inputstream is=conn.getinputstream(); 
    inputstreamreader isr=new inputstreamreader(is,"utf-8"); 
    bufferedreader br=new bufferedreader(isr); 
    buffer=new stringbuffer(); 
    string line=null; 
    while((line=br.readline())!=null){ 
      buffer.append(line); 
    } 
    }catch(exception e){ 
      e.printstacktrace(); 
    } 
    return buffer.tostring(); 
  } 

https请求:

1、https和http的区别我这里就不介绍了,在java 中访问https链接的话需要有相应的ssl证书,如果没有就无法访问(使用http访问https会返回403),所以我们要先自定义一个信任管理器。实现java自带的x509trustmanger接口,代码:

import java.security.cert.certificateexception; 
import java.security.cert.x509certificate; 
import javax.net.ssl.x509trustmanager; 
public class myx509trustmanager implements x509trustmanager { 
  @override 
  public void checkclienttrusted(x509certificate[] chain, string authtype) 
      throws certificateexception { 
    // todo auto-generated method stub 
  } 
  @override 
  public void checkservertrusted(x509certificate[] chain, string authtype) 
      throws certificateexception { 
    // todo auto-generated method stub 
  } 
  @override 
  public x509certificate[] getacceptedissuers() { 
    // todo auto-generated method stub 
    return null; 
  } 
} 

2、然后我们就可以使用https请求了:

/* 
 * 处理https get/post请求 
 * 请求地址、请求方法、参数 
 * */ 
public static string httpsrequest(string requesturl,string requestmethod,string outputstr){ 
  stringbuffer buffer=null; 
  try{ 
  //创建sslcontext 
  sslcontext sslcontext=sslcontext.getinstance("ssl"); 
  trustmanager[] tm={new myx509trustmanager()}; 
  //初始化 
  sslcontext.init(null, tm, new java.security.securerandom());; 
  //获取sslsocketfactory对象 
  sslsocketfactory ssf=sslcontext.getsocketfactory(); 
  url url=new url(requesturl); 
  httpsurlconnection conn=(httpsurlconnection)url.openconnection(); 
  conn.setdooutput(true); 
  conn.setdoinput(true); 
  conn.setusecaches(false); 
  conn.setrequestmethod(requestmethod); 
  //设置当前实例使用的sslsoctetfactory 
  conn.setsslsocketfactory(ssf); 
  conn.connect(); 
  //往服务器端写内容 
  if(null!=outputstr){ 
    outputstream os=conn.getoutputstream(); 
    os.write(outputstr.getbytes("utf-8")); 
    os.close(); 
  } 
  //读取服务器端返回的内容 
  inputstream is=conn.getinputstream(); 
  inputstreamreader isr=new inputstreamreader(is,"utf-8"); 
  bufferedreader br=new bufferedreader(isr); 
  buffer=new stringbuffer(); 
  string line=null; 
  while((line=br.readline())!=null){ 
    buffer.append(line); 
  } 
  }catch(exception e){ 
    e.printstacktrace(); 
  } 
  return buffer.tostring(); 
} 

以上这篇java 发送http和https请求的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: