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

java使用post方式调用webService接口如何传递对象型的参数?
2024-12-19 00:04:14
推荐回答(3个)
回答1:

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:

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();
}
}

回答3:

你说的是http请求吗?用http的话,接收参数和web请求一样的接收。