java中,如何用POST方法将参数传递给第三方网站

java中,如何用POST方法将参数传递给第三方网站,第1张

使用org.apache.commons.httpclient方便,效率又高,下面是post方式提交登录参数的代码:

public class FormLoginDemo

{

static final String LOGON_SITE = "developer.java.sun.com"

static final intLOGON_PORT = 80

public FormLoginDemo() {

super()

}

public static void main(String[] args) throws Exception {

HttpClient client = new HttpClient()

client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http")

PostMethod authpost = new PostMethod("/servlet/SessionServlet")

// 准备登录参数

NameValuePair action = new NameValuePair("action", "login")

NameValuePair url = new NameValuePair("url", "/index.html")

NameValuePair userid = new NameValuePair("UserId", "userid")

NameValuePair password = new NameValuePair("Password", "password")

authpost.setRequestBody(

new NameValuePair[] {action, url, userid, password})

// 执行Post请求

client.executeMethod(authpost)

// 打印状态码

System.out.println("Login form post: " + authpost.getStatusLine().toString())

// 释放连接

authpost.releaseConnection()

}

}

//前端要调用后端的接口可以是用jsonp

前端写法:

$.ajax({

    url:"/xxx.java",

    data:{type:"xxx",a:"aaa"},

    async: false,

    dataType: "jsonp",

    jsonp: "callback",

    jsonpCallback: "returnLiveViewData",

    success: function (msg) {

    

    },

    error:function(){

       alert(请求失败) 

    }

}

//后端返回结果

"returnLiveViewData({"records": [{"name": "aaa", "IP": "11.11.11.11", "node": 114, },{"name": "bob", "IP": "11.11.11.22", "node": 115, },]}

)"

这跟java没关系吧,做一个页面的form提交就可以

<form action="***" method="post" target="_blank">

这里写你要提交的参数

</form>

然后提交你这个form就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9941888.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存