1.服务端远程服务
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.constants.Style;
import org.apache.axis.constants.Use;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import javax.xml.namespace.QName;
import java.net.URL;
import java.util.Vector;
public class Demo {
public static void main(String[] args) throws Exception {
getWebServiceInfo("http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx");
}
public static String getWebServiceInfo(String url) throws Exception {
OperationDesc oper = new OperationDesc();
oper.setName("getStationName");
ParameterDesc param;
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(url));
QName qname = new QName("http://WebXml.com.cn/", "getStationName");
call.setOperationName(qname);
oper.setReturnType(new QName("http://xml.apache.org/xml-soap", "Vector"));
oper.setStyle(Style.WRAPPED);
oper.setUse(Use.LITERAL);
call.setOperation(oper);
call.setSOAPActionURI("http://WebXml.com.cn/getStationName");
call.setUseSOAPAction(true);
Vector<String> Msg = (Vector<String>) call.invoke(new Object[]{});
System.out.println(Msg);
return null;
}
}
2.客户端远程服务
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientHandler {
private static final String URL = "http://127.0.0.1:port/api";
public static String sendPost(String param) throws Exception {
try {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(150000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(150000);
PostMethod postMethod = new PostMethod(URL);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
postMethod.setRequestEntity(new StringRequestEntity(param, "text/xml", "UTF-8"));
httpClient.executeMethod(postMethod);
String result = null;
if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
result = postMethod.getResponseBodyAsString();
System.out.println("responseResult:" + result);
} else {
System.out.println("method.getStatusCode()" + postMethod.getStatusCode());
}
postMethod.releaseConnection();
return result;
} catch (HttpException e) {
e.printStackTrace();
throw new HttpException(e.getMessage());
}
}
}
3.HttpClient
public static String doPostJson(String url, String json) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doHttpPost(String url, Map map) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = "";
try {
HttpPost post = new HttpPost(url);
List<NameValuePair> formList = new ArrayList<>();
formList.add(new BasicNameValuePair("name", "张三"));
formList.add(new BasicNameValuePair("pass", "1243"));
StringEntity entity = new UrlEncodedFormEntity(formList, "utf-8");
post.setEntity(entity);
response = httpClient.execute(post);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String doHttpGet(String url, Map map) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.setParameter("key", "value");
URI remoteUrl = uriBuilder.build();
HttpGet httpGet = new HttpGet(remoteUrl);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}