0
点赞
收藏
分享

微信扫一扫

java NameValuePair 实现 模拟 表单访问 服务 传输各类型数据


背景

     今天通过api接口访问服务端,但是一直提示发送参数为空,后来使用NameValuePair 发送成功,记录在此,方便以后使用。

 

发送表单数据

/**
* 发送消息
*
* @param url 地址
* @param param 参数
* @throws IOException
*/
public String sendPostRequest(String url,NameValuePair[] param) throws HttpException, IOException {
String responseStr = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("accept", "*/*");
postMethod.setRequestHeader("connection", "Keep-Alive");
postMethod.setRequestHeader("yonghong-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 将表单的值放入postMethod中
postMethod.setRequestBody(param);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
BufferedReader in = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
int len;
while ((len = in.read()) != -1) {
sb.append((char) len);
}
responseStr = sb.toString();
in.close();
postMethod.releaseConnection();
return responseStr;
}

 

编辑一个xml类型数据

 data = "<?xml version='1.0' encoding='UTF-8'?>\n"
+"<info>\n"
+" <user>\n"
+" <name>"+userName+"</name>\n"
+" <pass>"+password+"</pass>\n"
+" <email></email>\n"
+" <alias>"+userName+"</alias>\n"
+" <parent></parent>\n"
+" <roles>xx</roles>\n"
+" </user>\n"
+"</info>";
param = new NameValuePair[]{
new NameValuePair("xmlData", data)
};
url = biAddress + "/serxasasce/aasasaspi?action=saveNode&type=user&token=" + token + "&IS=" + UUID.randomUUID();
System.out.println(sendPostRequest(url, param));

 

示例:


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.UUID;

/**
* 执行表单任务
*
*
*/
public class ExecuteFrom {

/**
* 发送消息
*
* @param url 地址
* @param param 参数
* @throws IOException
*/
public String sendPostRequest(String url,NameValuePair[] param) throws HttpException, IOException {
String responseStr = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("accept", "*/*");
postMethod.setRequestHeader("connection", "Keep-Alive");
postMethod.setRequestHeader("yonghong-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 将表单的值放入postMethod中
postMethod.setRequestBody(param);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
BufferedReader in = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
int len;
while ((len = in.read()) != -1) {
sb.append((char) len);
}
responseStr = sb.toString();
in.close();
postMethod.releaseConnection();
return responseStr;
}

//ready porject
public static void main(String[] args) throws Exception{
new ExecuteFrom().action(0);
}

/**
* 执行任务传入xml等数据
*
* @param i
* @throws HttpException
* @throws IOException
*/
public void action(int i) throws HttpException, IOException {

final String biAddress = "http://101.110.11.94:8080";

String adminName = "admsdin";
String adminPassword = "Bxsdsd526";

String userName = "wasdsdwu";
String password = "abcdsdsdfj111!";

//获取token
String url = biAddress + "/bsdsdi/apsdsdi?action=login&adminv=" + adminName + "&passv=" + adminPassword + "&IS=" + UUID.randomUUID();
NameValuePair[] param = new NameValuePair[]{};
String result = sendPostRequest(url, param);
String token = result.substring(result.indexOf("<message>")+"<message>".length(), result.indexOf("</message>"));
System.out.println(token);
String data = "";
String path = "";
if(i == 0) {
//创建用户
data = "<?xml version='1.0' encoding='UTF-8'?>\n"
+"<info>\n"
+" <user isOverWrite='true'>\n"
+" <name>"+userName+"</name>\n"
+" <pass>"+password+"</pass>\n"
+" <email></email>\n"
+" <alias>"+userName+"</alias>\n"
+" <parent></parent>\n"
+" <roles>developer_role</roles>\n"
+" </user>\n"
+"</info>";
param = new NameValuePair[]{
new NameValuePair("xmlData", data)
};
url = biAddress + "/bi/api?action=saveNode&type=user&token=" + token + "&IS=" + UUID.randomUUID();
System.out.println(sendPostRequest(url, param));
}else if(i == 1) {
//获取用户信息
data = "<?xml version='1.0' encoding='UTF-8'?>\n"
+"<ref>\n"
+" <type>user</type>\n"
+"</ref>";
param = new NameValuePair[]{
new NameValuePair("xmlData", data)
};
url = biAddress + "/bi/api?action=getNode&returnPwd=no&token=" + token + "&IS=" + UUID.randomUUID();
System.out.println(sendPostRequest(url, param));
}else if(i == 2) {
//登出
url = biAddress +"/bi/api?action=logout&IS=" + UUID.randomUUID();
param = new NameValuePair[]{
new NameValuePair("token", token)
};
System.out.println(sendPostRequest(url, param));
}

}

}

 

api介绍:

构造函数摘要

NameValuePair()     默认构造函数。

 

NameValuePair(String name, String value)   构造函数。

 

方法摘要

 boolean

equals(Object object)  

 String

getName()   返回名称。

 String

getValue()     返回当前值。

 int

hashCode()     

 void

setName(String name)   设置名称。

 void

setValue(String value)     设置值。

 String

toString()    获取此对的字符串表示形式。

从类java.lang继承的方法 

clonefinalizegetClassnotifynotifyAllwaitwaitwait

 

构造函数详细信息

 NameValuePair

public NameValuePair()

默认构造函数。

 

NameValuePair

public NameValuePairString  name, String  value)

构造函数。

 

参数:

name - 名字。

value - 价值。

方法细节

setName

public void setNameString  name)

设置名称。

 

参数:

name - 新名字

也可以看看:

getName()

getName

public String  getName()

返回名称。

 

返回:

字符串名称名称

也可以看看:

setName(String)

设定值

public void setValueString  value)

设置值。

 

参数:

value - 新价值。

的getValue

public String  getValue()

返回当前值。

 

返回:

字符串值当前值。

toString

public String  toString()

获取此对的字符串表示形式。

 

覆盖:

toString 在班上 Object

返回:

字符串表示。

等于

public boolean equalsObject  object)

覆盖:

equals 在班上 Object

hashCode

public int hashCode()

覆盖:

hashCode 在班上 Object

 

 

 

 

持续更新

 

 

 

 

 

举报

相关推荐

0 条评论