WebService可以有Get、 Post、Soap、Document四种方式调用,以下Java通过post方式调用WebService代码:
import java.io.BufferedReaderimport java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
import java.net.URLEncoder
import org.apache.cxf.endpoint.Client
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory
/**
* 功能描述:WebService调用
*
*/
public class ClientTest {
/**
* 功能描述:HTTP-POST
*
*/
public String post() {
OutputStreamWriter out = null
StringBuilder sTotalString = new StringBuilder()
try {
URL urlTemp = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity")
URLConnection connection = urlTemp.openConnection()
connection.setDoOutput(true)
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")
StringBuffer sb = new StringBuffer()
sb.append("byProvinceName=福建")
out.write(sb.toString())
out.flush()
String sCurrentLine
sCurrentLine = ""
InputStream l_urlStream
l_urlStream = connection.getInputStream()// 请求
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
l_urlStream))
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString.append(sCurrentLine)
}
} catch (Exception e) {
e.printStackTrace()
} finally {
if (null != out) {
try {
out.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
return sTotalString.toString()
}
}
str=>jsoneval('(' + str + ')')
可以使用$.ajax方法 直接打印出str
$.ajax({
type:"post",dataType:"json",url:"WebService.asmx/B",success:function(json){alert(json.name)}
})
----
[WebMethod]
public string B()
{
string str = "[{\"name\":\"Mike\"}]"
HttpResponse response = HttpContext.Current.Response
response.Write(str)
response.End()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)