java语言使用post方式调用webService方式

java使用post方式调用webService接口如何传递对象型的参数?

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();
 }
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-16
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();
}
}
第2个回答  2014-06-11
你说的是http请求吗?用http的话,接收参数和web请求一样的接收。本回答被提问者采纳