网上看了好多代码,直接复制过来好像就能用,可是总是用的不够坦荡,毕竟是拿来主义,因为不懂里面具体的内容。也不知道怎么处理,接下来我就讲解具体的解决思路与关键点。
咱们用的服务器编写代码去访问另一个服务。而且这个服务是webservice服务。那就要先了解底层是用的什么技术这个问题?用的是SOAP(简单对象访问协议),具体点就是HTTP+XML。
在这里先了解下什么是HTTP,http是如何获取服务器上的资源的,服务器请求是怎样的?答:客户端 发送请求,这个请求是包含请求行,请求头,请求体的 ,是打包在一起发给 服务器的。其中请求头可以自定义好多东西,其中最重要的就是请求头中的ContenType,顾名思义就是--内容类型 ,什么内容呢?请求体中的内容类型,请求体都有什么诶荣类型呢?有普通文本类型
text/plain
(这个是默认值),你要把请求体想成一个文件,服务器看到ContenType就能知道它要怎么解析里面的内容,这个contenType还包括编码方式,平时用的最多的就是 contenType:"text/xml; charset=utf-8"。
回归到正题,soap的方式是http+xml,意思是请求体要用 xml的方式,当然请求体是xml方式,那就别忘了设置contType为xml的方式,这样才是正解。才是好多像我这样的小白迷惑的地方。具体去哪儿看到底用哪种类型,直接去参考别人的网址就明白了,在这里无私奉上:https://www.cnblogs.com/garfieldcgf/p/5966317.html
同理,它返回来的也是xml的格式,自己再解析下就行,比如通过subString() 切词方法就能拿到你自己想要的最终结果。
如果觉得对您有用,麻烦给个赞,谢谢!
介绍之前 先了解下 http的知识,包含请求和响应的本质详细 ,得提前学下 请求头和响应头的知识 这个可以看我的 百度脑图 来学习
总结:也是http请求 ,只不过内容是xml格式的就可以。
先看一下HTTP的方式:
请求消息:
POST
/
item HTTP
/
1.1
Host
:
189.123
.
255.239
Content
-
Type
:
text
/
plain
Content
-
Length
:
200
随后服务器会处理此请求,然后向客户机发送一个 HTTP 响应。此响应包含了可指示请求状态的状态代码:
200
OK
Content
-
Type
:
text
/
plain
Content
-
Length
:
200
再看SOAP HTTP Binding(绑定)的方式:
SOAP 方法指的是遵守 SOAP 编码规则的 HTTP 请求/响应。
HTTP + XML = SOAP
SOAP 请求可能是 HTTP POST 或 HTTP GET 请求。
HTTP POST 请求规定至少两个 HTTP 头:Content-Type 和 Content-Length。
Content-Type
SOAP 的请求和响应的 Content-Type 头可定义消息的 MIME 类型,以及用于请求或响应的 XML 主体的字符编码(可选)。
语法
Content
-
Type
:
MIMEType
;
charset
=
character
-
encoding
实例
POST
/
item HTTP
/
1.1
Content
-
Type
:
application
/
soap
+
xml
;
charset
=
utf
-
8
Content-Length
SOAP 的请求和响应的 Content-Length 头规定请求或响应主体的字节数。
语法
Content
-
Length
:
bytes
实例
POST
/
item HTTP
/
1.1
Content
-
Type
:
application
/
soap
+
xml
;
charset
=
utf
-
8
Content
-
Length
:
250
再最后直接上最简单的代码(网上复制粘贴的):
代码准备:
1.网络上有提供一些免费的服务器测试地址,可以上这里找一找:https://my.oschina.net/CraneHe/blog/183471
2.1打开之后看到该地址下有一个方法:
2.2点击进入,网站会提供该方法的客户端请求xml格式:
2.3,这个红框部分就是我们要的,将它写入代码,就可以完成请求了.
注意:以上还是获取soap请求xml的方法,也是比较入门的方式,有经验的筒子直接上wsdl利用解释文档也可以自己写xml...
然后是代码,我直接附上代码,大家直接复制即可运行,附注释.
Translate.class
import
java.io.ByteArrayOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.net.HttpURLConnection;
import
java.net.URL;
/**
* Created by garfield on 2016/10/16.
*/
public
class
Translate {
public
static
void
translate(String word )
throws
Exception {
//
地址
//
方法
URL url
=
new
URL(urlString);
HttpURLConnection httpConn
=
(HttpURLConnection) url.openConnection();
//
拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接(直接将网页上的复制进来即可)
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope xmlns:xsi=\"
http://www.w3.org/2001/XMLSchema-instance
\" xmlns:xsd=\"
http://www.w3.org/2001/XMLSchema
\" xmlns:soap=\"
http://schemas.xmlsoap.org/soap/envelope/
\">\n" +
" <soap:Body>\n" +
" <Word>" + word + "</Word>\n" +
" </getEnCnTwoWayTranslator>\n" +
" </soap:Body>\n" +
"</soap:Envelope>"
;
byte
[] buf =
soap.getBytes();
//
设置一些头参数
httpConn.setRequestProperty("Content-Length"
, String.valueOf(buf.length));
httpConn.setRequestProperty(
"Content-Type", "text/xml; charset=utf-8"
);
httpConn.setRequestProperty(
"soapActionString"
, soapActionString);
httpConn.setRequestMethod(
"POST"
);
//
输入参数和输出结果
httpConn.setDoOutput(
true
);
httpConn.setDoInput(
true
);
OutputStream out
=
httpConn.getOutputStream();
out.write(buf);
out.close();
//
最后合格解析结果大家就各显神通了,此处打印出解析的过程,最终得到翻译答案
byte
[] datas =
readInputStream(httpConn.getInputStream());
String result
=
new
String(datas);
System.out.println(
"result:" +
result);
System.out.println(result.substring(result.indexOf(
"string") - 1,result.lastIndexOf("string") + 7
));
System.out.println(result.substring(result.indexOf(
"string") - 1,result.lastIndexOf("string") + 7).replaceAll("</{0,1}(string)?>",""
));
}
/**
* 从输入流中读取数据
*
*
@param
inStream
*
@return
*
@throws
Exception
*/
public
static
byte
[] readInputStream(InputStream inStream)
throws
Exception {
ByteArrayOutputStream outStream
=
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[1024
];
int
len = 0
;
while
((len = inStream.read(buffer)) != -1
) {
outStream.write(buffer,
0
, len);
}
byte
[] data =
outStream.toByteArray();
outStream.close();
inStream.close();
return
data;
}
public
static
void
main(String[] args)
throws
Exception {
translate(
"sea"
);
}
}
运行结果:
result:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/
" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
" xmlns:xsd="
http://www.w3.org/2001/XMLSchema
"><soap:Body><getEnCnTwoWayTranslatorResponse xmlns="
http://WebXml.com.cn/
"><getEnCnTwoWayTranslatorResult><string>sea: [ si: ]</string><string>n. 海,海洋 |</string></getEnCnTwoWayTranslatorResult></getEnCnTwoWayTranslatorResponse></soap:Body></soap:Envelope>
<string>sea: [ si: ]</string><string>n. 海,海洋 |</string>
sea: [ si: ]n. 海,海洋
|
第一行是直接返回的结果,下面两行帮助理解解析,最后得到sea单词的解释,是不是简单清楚...
第二期补充:有筒子可能有问题,那我要写的soap只有wsdl地址怎么办,而且还要求有请求头验证,这个我也找了之前写的一个请求代码,同样非常简单,用到的jar包只有httpclient
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
TestService.java
import
java.io.ByteArrayInputStream;
import
java.io.InputStream;
import
java.util.HashMap;
import
java.util.Map;
/**
* Created by garfield on 2016/10/12.
*/
public
class
TestWebService {
public
static
void
main(String[] args)
throws
Exception {
Map
<String, String> map =
new
HashMap<String, String>
();
//
拼接xml请求,带有请求头
String params = "<id>5</id>";
//
随手举个例子,类似...
String soapRequestData = "<soapenv:Envelope \n" +
" <soapenv:Header>\n" +
"\t<serviceCode>serviceCode</serviceCode>\n" +
"\t<userName>userName</userName>\n" +
"\t<authCode>authCode</authCode>\n" +
" </soapenv:Header>\n" +
" <soapenv:Body>\n" +
" <ser:function>\n" +
params
+
" </ser:function>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>\n"
;
try
{
String method
= "请求地址";
//
比如http:
//
192.177.222.222:8888/services/Service_Name/Function_Name
PostMethod postMethod =
new
PostMethod(method);
byte
[] b = soapRequestData.getBytes("utf-8"
);
InputStream is
=
new
ByteArrayInputStream(b, 0
, b.length);
RequestEntity re
=
new
InputStreamRequestEntity(is, b.length, "application/soap+xml; charset=utf-8"
);
postMethod.setRequestEntity(re);
HttpClient httpClient
=
new
HttpClient();
int
statusCode =
httpClient.executeMethod(postMethod);
//
200说明正常返回数据
if
(statusCode != 200
) {
//
internet error
System.out.println(statusCode);
}
soapRequestData
=
postMethod.getResponseBodyAsString();
System.out.println(soapRequestData);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
好了,将这个简单的代码复制进去,替换一下请求头和请求地址以及参数就可以得到反馈就过了,试用一下吧.
本文介绍了如何使用HTTPClient请求webService,并探讨了webService如何处理SOAP请求。内容涉及Content-Type和Content-Length的语法及实例。

7775

被折叠的 条评论
为什么被折叠?



