java语言使用post方式调用webService方式
3个回答
2015-07-06 · 知道合伙人互联网行家
关注
展开全部
WebService可以有Get、 Post、Soap、Document四种方式调用,以下Java通过post方式调用WebService代码:
import java.io.BufferedReader;
import 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();
}
}
展开全部
WebService可以有Get、 Post、Soap、Document四种方式调用,以下Java通过post方式调用WebService代码:
import java.io.BufferedReader;
import 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();
}
}
import java.io.BufferedReader;
import 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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你说的是http请求吗?用http的话,接收参数和web请求一样的接收。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询