利用HttpURLConnection通过soap调用接口:
public static void cl6(String xml) throws IOException {
// 服务器地址
String urlString = ";;
// 需要调用的方法
String soapActionString = "getMobileCodeInfo";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 拼接soap
String soap = xml;
byte[] buf = ("UTF-8");
// 设置报头
conn.setRequestProperty("Content-Length", S));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("soapActionString", soapActionString);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream out = conn.getOutputStream();
out.write(buf);
out.close();
// 获取响应状态码
int code = conn.getResponseCode();
StringBuffer sb = new StringBuffer();
if (code == H()) {
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len, "utf-8");
(s);
}
is.close();
}
Sy(sb);
}
public static void main(String args[]) throws IOException {
String str =
"<?xml version=/"1.0/" encoding=/"utf-8/"?>/n"
+ "/n"
+ "/n"
+ "/n"
+ "/n"
+ "/n"
+ "/n"
+ "/n"
+ "";
cl6(str);
}
使用HttpClient
public static void cl3() throws IOException {
// 输入服务网址
HttpClient client = new HttpClient();
// HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
// GetMethod
PostMethod post = new PostMethod(";);
// 设置参数
("mobileCode", "");
("userID", "");
// client.setTimeout(newTimeoutInMilliseconds);
// 执行,返回一个结果码
int code = client.executeMethod(post);
Sy("结果码:" + code);
// 获取xml结果
String result = ();
Sy("结果:" + result);
// 释放连接
();
// 关闭连接
((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
}
使用jar
commons-httpclient
commons-httpclient
3.1
架构师视频资料分享链接:
data:text/html;charset=UTF-8;base64,
5p625p6E5biI5a2m5Lmg5Lqk5rWB576k5Y+35pivNjk2NTU1NTMyCg==
复制粘贴在网站即可!
共勉