background

**** accessed the server through API interface today, but kept prompting that the sending parameter was empty. Later, NameValuePair was used to send successfully, which is recorded here for future use.

 

Sending form data

/** * Send messages ** @param URL * @param param parameter * @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"); / / the value of the form into the 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; }Copy the code

 

Edit an XML type data

data = "<? The 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));Copy the code

 

Example:

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; /** * Performs form tasks ** */ public class ExecuteFrom {/** ** Sends messages ** @param URL * @param param parameter * @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"); / / the value of the form into the 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); } /** * Data such as XML is passed in to perform a task ** @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!" ; 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) {// create user data = "? The 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 = "<? The 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) {// add url = biAddress +"/bi/ API? action=logout&IS=" + UUID.randomUUID(); param = new NameValuePair[]{ new NameValuePair("token", token) }; System.out.println(sendPostRequest(url, param)); }}}Copy the code

 

The API is introduced:

Constructor Abstract
NameValuePair() Default constructor.  
NameValuePair(String name, StringValue) constructor.  

 

Methods in this paper,
 boolean equals(Object object)  
 String getName() Returns the name.
 String getValueReturns the current value.
 int hashCode(a)
 void setName(StringName) sets the name.
 void setValue(StringValue) Sets the value.
 String toStringGets the string representation of the pair.

 

Methods inherited from the java.lang class
clone.finalize.getClass.notify.notifyAll.wait.wait.wait

 

Constructor details

NameValuePair

Public NameValuePair ()

Default constructor.

 


NameValuePair

Public NameValuePair (String name, String value)

Constructor.

 

Parameters:

Name – Name.

Value – Value.

Methods the detail

setName

Public void setName (String name)

Set the name.

 

Parameters:

Name – New name

Also check out:

getName()


getName

Public String getName ()

Returns the name.

 

Returns:

String Name Name

Also check out:

setName(String)


The set value

Public void setValue (String value)

Set the value.

 

Parameters:

Value – New value.


The getValue

Public String getValue ()

Returns the current value.

 

Returns:

String value Current value.


toString

Public String toString ()

Gets the string representation of the pair.

 

Cover:

ToString is in the class Object

Returns:

String representation.


Is equal to the

Public Boolean equals (Object Object)

Cover:

Equals is in the class Object


hashCode

Public int hashCode ()

Cover:

HashCode in class Object

 

 

 

 

Continuously updated