java webservice 通过什么传递
2个回答
展开全部
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(
"url..");
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(
"url..");
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询