java实现Get、Put、Delete⽅式发送xml格式数据的http协议今天⼯作之余把最近实现的功能在记录⼀下,⽅便以后查。
导⼊需要的包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
下⾯请求的url中需要⽤到服务端ip地址和端⼝号
//从配置⽂件中获取服务端ip地址
@Value("${poc.server.ip}")
private String serverIp;
private final String serverPort="8866";
private final String userProfilePort="8070";
封装Xml格式数据的内容,这个xml内容稍复杂。
* @param groupNumber 后台⽣成的组号码
* @param callerSsi 发起⽅号码
* @return xml
*/
private String getRequestBody(String groupNumber, String callerSsi){
Document doc = ateDocument();
//创建根元素
Element root = doc.addElement("group","urn:oma:xml:poc:list-service");
//元素创建属性
root.addAttribute("xmlns:rl", "urn:ietf:params:xml:ns:resource-lists").addNamespace("rl", "urn:ietf:params:xml:ns:resource-lists");
root.addAttribute("xmlns:cp", "urn:ietf:params:xml:ns:common-policy").addNamespace("cp", "urn:ietf:params:xml:ns:common-policy"); root.addAttribute("xmlns:ocp", "urn:oma:xml:xdm:common-
policy").addNamespace("ocp", "urn:oma:xml:xdm:common-policy");
root.addAttribute("xmlns:oxe", "urn:oma:xml:xdm:extensions").addNamespace("oxe", "urn:oma:xml:xdm:extensions");
root.addAttribute("xmlns:mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0").addNamespace("mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0"); //创建⼦元素
Element listService = root.addElement("list-service","");
listService.addAttribute("uri", "sip:"+groupNumber+"@s");
//给⼦元素创建⼦元素
Element displayName = listService.addElement("display-name");
displayName.addAttribute("xml:lang", "en-us");
displayName.addText("test");
listService.addElement("mcpttgi:on-network-invite-members").addText("true");
listService.addElement("mcpttgi:on-network-max-participant-count").addText("10");
Element cpRuleset = listService.addElement("cp:ruleset");
Element cpRule = cpRuleset.addElement("cp:rule");
cpRule.addAttribute("id", "a7c");
Element cpConditions = cpRule.addElement("cp:conditions");
cpConditions.addElement("is-list-member");
Element cpActions = cpRule.addElement("cp:actions");
cpActions.addElement("allow-initiate-conference").addText("false");
cpActions.addElement("join-handling").addText("false");
cpActions.addElement("mcpttgi:allow-MCPTT-emergency-call").addText("true");
cpActions.addElement("mcpttgi:allow-imminent-peril-call").addText("true");
Element supServices = listService.addElement("oxe:supported-services");
Element oxeService = supServices.addElement("oxe:service");
oxeService.addAttribute("enabler", "urn:urn-7:3gpp-service.ptt");
Element groupMedia = oxeService.addElement("oxe:group-media");
groupMedia.addElement("mcpttgi:mcptt-speech");
listService.addElement("on-network-disabled").addText("false");
listService.addElement("on-network-group-priority").addText("0");
listService.addElement("owner").addText(callerSsi);
return doc.asXML();
}
发送Get⽅式的请求,请求参数在url中传递
* @param type 呼叫类型
* @param callerSsi 发起⽅号码
*/
public void getUserProfile(String type,String callerSsi) {
CloseableHttpResponse httpResponse =null;
String urlType="";
String xmlType="";
String gppType="";
try {
if("mcptt".equals(type)) {
urlType="@s";
xmlType="/l";
gppType="mcptt";
}else if("mcdata".equals(type)) {
urlType="@s";
xmlType="/l";
gppType="mcdata";
}else if("mcvideo".equals(type)) {
urlType="@s";
xmlType="/l";
gppType="mcvideo";
}
URI uri = new URI(""+serverIp+":"+userProfilePort+"/mcs/sip:"+callerSsi+urlType+"//mcs/sip:"+callerSsi+urlType+ "/org.3gpp."+gppType+".user-profile/users/sip:"+callerSsi+urlType+xmlType);
HttpGet httpGet = new HttpGet(uri);
httpGet.setHeader("Content-Type", "text/xml");
httpGet.addHeader("User-Agent", "XDM-client/OMA2.1");
CloseableHttpClient httpClient = ateDefault();
httpResponse = ute(httpGet);
//获取返回的结果信息
String resultMsg = Entity(),"UTF-8");
//获取响应码
int statusCode = StatusLine().getStatusCode();
logger.info("Response UserProfile statusCode:{};type:{};Content:{};",statusCode,type,resultMsg);
} catch (Exception e) {
e.printStackTrace();
<("Response UserProfile Error!",e);
}
}
发送Put⽅式的请求
public void updateGroupData() {
CloseableHttpResponse httpResponse =null;
String groupNumber="600105";
String callerSsi="800101";
try {
String urlType="@s";
String requestBody = getRequestBody(groupNumber, callerSsi);
URI uri = new URI(""+serverIp+":"+serverPort+"/path1/ups/users/sip:"+callerSsi+urlType+"/l&operate=mod HttpPut httpPut = new HttpPut(uri);
httpPut.setHeader("Content-Type", "text/xml");
httpPut.addHeader("User-Agent", "okhttp/3.6.0");
httpPut.setEntity(new StringEntity(requestBody,"UTF-8"));
CloseableHttpClient httpClient = ateDefault();
httpResponse = ute(httpPut);
int code = StatusLine().getStatusCode();
logger.info("ModifyGroup SendRequest End code:{};groupNumber:{};",code,groupNumber);
} catch (Exception e) {
e.printStackTrace();
<("ModifyGroup Error!",e);
}
}
发送Delete⽅式的请求,Delete⽅式不能直接在Entity中封装参数,需要实现⼀个⾃定义的类继承http的基类
⾃定义的Http请求的Delete类
public class HttpDeleteMethod extends HttpEntityEnclosingRequestBase{
public final static String METHOD_NAME = "DELETE";
@Override
public String getMethod() {
return METHOD_NAME;
}
public HttpDeleteMethod() {
使用dom4j解析xml文件super();
}
public HttpDeleteMethod(final URI uri) {
super();
setURI(uri);
}
/**
* @throws IllegalArgumentException if the uri is invalid.
*/
public HttpDeleteMethod(final String uri) {
super();
ate(uri));
}
}
实现Delete⽅式封装参数发送
public int deleteGroup() {
CloseableHttpResponse httpResponse =null;
String groupNumber="600105";
String callerSsi="800101";
try {
URI uri = new URI(""+serverIp+":"+serverPort+"/path1/ups/users/sip:"+callerSsi+"@l"); HttpDeleteMethod httpDelete = new HttpDeleteMethod(uri);
httpDelete.setHeader("Content-Type", "text/xml");
httpDelete.addHeader("User-Agent", "okhttp/3.6.0");
//封ody内容
Document doc = ateDocument();
Element root = doc.addElement("group","urn:oma:xml:poc:list-service");
root.addAttribute("xmlns:rl", "urn:ietf:params:xml:ns:resource-lists").addNamespace("rl", "urn:ietf:params:xml:ns:resource-lists");
root.addAttribute("xmlns:cp", "urn:ietf:params:xml:ns:common-policy").addNamespace("cp", "urn:ietf:params:xml:ns:common-policy");
root.addAttribute("xmlns:ocp", "urn:oma:xml:xdm:common-policy").addNamespace("ocp", "urn:oma:xml:xdm:common-policy");
root.addAttribute("xmlns:oxe", "urn:oma:xml:xdm:extensions").addNamespace("oxe", "urn:oma:xml:xdm:extensions");
root.addAttribute("xmlns:mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0").addNamespace("mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0");
Element listService = root.addElement("list-service","");
listService.addAttribute("uri", "sip:"+groupNumber+"@group");
String requestBody=doc.asXML();
httpDelete.setEntity(new StringEntity(requestBody,"UTF-8"));
CloseableHttpClient httpClient = ateDefault();
httpResponse = ute(httpDelete);
//String resultMsg = Entity(),"UTF-8");
int statusCode = StatusLine().getStatusCode();
logger.info("DeleteGroup statusCode:{};groupNumber:{};",statusCode,groupNumber);
return statusCode;
} catch (Exception e) {
e.printStackTrace();
<("DeleteGroup Error!",e);
return -1;
}
}
好了,这样三种⽅式就实现完了。需要注意的细节就是Delete⽅式传递参数需要⾃⼰实现。封装xml格式的时候添加完属性⼀定要加namespace,不然封装的xml格式就不对了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论