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

Java中Https发送POST请求[亲测可用]

程序员文章站 2022-03-07 09:39:25
1、直接建一个工具类放入即可/** * 发送https请求共用体 */public static jsonobject sendpost(string url,string parame,map...

1、直接建一个工具类放入即可

/**
 * 发送https请求共用体 
 */
public  static jsonobject  sendpost(string url,string parame,map<string,object> pmap) throws ioexception, keymanagementexception, nosuchalgorithmexception, nosuchproviderexception{
    // 请求结果
    jsonobject json = new jsonobject();
    printwriter out = null;
    bufferedreader in = null;
    string result = "";
    url realurl;
    httpsurlconnection conn;
    string method = "post";
    //查询地址
    string querystring = url;
    //请求参数获取
    string postpar = "";
    //字符串请求参数
    if(parame!=null){
        postpar = parame;
    }
    // map格式的请求参数
    if(pmap!=null){
        stringbuffer mstr = new stringbuffer();
        for(string str:pmap.keyset()){
            string val = (string) pmap.get(str);
            try {
                val=urlencoder.encode(val,"utf-8");
            } catch (unsupportedencodingexception e) {
                e.printstacktrace();
            }
                mstr.append(str+"="+val+"&");
        }
        // 最终参数
        postpar = mstr.tostring(); 
        int lasts=postpar.lastindexof("&");
        postpar=postpar.substring(0, lasts);
    }
    if(method.touppercase().equals("get")){
        querystring+="?"+postpar;
    }
    sslsocketfactory  ssf= httpsclientutils.getssfactory();
    try {
        realurl= new url(querystring);
        conn = (httpsurlconnection)realurl.openconnection();
        conn.setsslsocketfactory(ssf);
        conn.setrequestproperty("content-type","application/x-www-form-urlencoded");
        conn.setrequestproperty("accept", "*/*");
        conn.setrequestproperty("connection", "keep-alive");
        conn.setrequestproperty("user-agent",
                "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
        if(method.touppercase().equals("post")){
            conn.setdooutput(true);
            conn.setdoinput(true);
            conn.setusecaches(false);
            out = new printwriter(conn.getoutputstream());
            out.print(postpar);
            out.flush();
        }else{
            conn.connect();
        }
        in = new bufferedreader(
                new inputstreamreader(conn.getinputstream(),"utf-8"));
        string line;
        while ((line = in.readline()) != null) {
            result += line;
        }
        json = jsonobject.fromobject(result);
    }finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (ioexception ex) {
            ex.printstacktrace();
        }
    }
    return json;
}

2、可能需要的包

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.io.unsupportedencodingexception;
import java.net.uri;
import java.net.url;
import java.net.urlencoder;
import java.security.keymanagementexception;
import java.security.nosuchalgorithmexception;
import java.security.nosuchproviderexception;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import java.util.random;
import java.util.set;
import java.util.treemap;

import javax.net.ssl.httpsurlconnection;
import javax.net.ssl.sslsocketfactory;

import net.sf.json.jsonobject;

import org.apache.commons.codec.digest.digestutils;
import org.apache.http.namevaluepair;
import org.apache.http.client.entity.urlencodedformentity;
import org.apache.http.client.methods.closeablehttpresponse;
import org.apache.http.client.methods.httpget;
import org.apache.http.client.methods.httppost;
import org.apache.http.client.utils.uribuilder;
import org.apache.http.entity.contenttype;
import org.apache.http.entity.stringentity;
import org.apache.http.impl.client.closeablehttpclient;
import org.apache.http.impl.client.httpclients;
import org.apache.http.message.basicnamevaluepair;
import org.apache.http.util.entityutils;

到此这篇关于java中https发送post请求[亲测可用]的文章就介绍到这了,更多相关https发送post请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!