绪论
杂七杂八,瞎记一下。
一、Http访问
由于这种方式可以直接在浏览器中使用,所以不是很安全,可以考虑把要传输的内容进行加密。
Java web项目,使用SpringMVC。
访问地址为: http://127.0.0.1:8080/zDemo/httpDemo/getCall
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/httpDemo")
public class HttpController {
@RequestMapping("getCall")
@ResponseBody
public String getCall(String name) throws UnsupportedEncodingException{
byte[] b = decryptBASE(name);
name = new String(b, "utf-8");
System.out.println(name + "访问了我");
name = "你好," + name;
name = encryptBASE(name.getBytes("utf-8"));
return name;
}
/**
* Base64 解密
*
* @param key
* @return
*/
public static byte[] decryptBASE(String key) {
return Base64.getDecoder().decode(key);
}
/**
* Base64 加密
*
* @param key
* @return
*/
public static String encryptBASE(byte[] key) {
return Base64.getEncoder().encodeToString(key);
}
}
Java访问代码
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class HttpInterface {
public static void main(String[] args) throws UnsupportedEncodingException {
String name = "张三";
name = encryptBASE(name.getBytes("utf-8"));
String s = getRequest("http://127.0.0.1:8080/zDemo/httpDemo/getCall?name="+name, "POST");
byte[] b = decryptBASE(s);
System.out.println(new String(b, "utf-8"));
}
/**
* 访问远程http
*
* @param requestUrl(访问地址)
* @param method(访问方式get、post)
* @return
*/
public static String getRequest(String requestUrl, String method) {
StringBuffer buffer = null;
try {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(method);
conn.connect();
// 读取服务器端返回的内容
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
return buffer.toString();
}
}
/**
* Base64 解密
*
* @param key
* @return
*/
public static byte[] decryptBASE(String key) {
return Base64.getDecoder().decode(key);
}
/**
* Base64 加密
*
* @param key
* @return
*/
public static String encryptBASE(byte[] key) {
return Base64.getEncoder().encodeToString(key);
}
}
为了防止乱码,我这里进行了base64编码。
运行结果:
二、Webservice访问
在Java项目中,访问Webservice可以使用axis或者xfire的相关jar包。
- axis:代码比xfire写起来麻烦,但是在跨语言访问上比较稳定。
- xfire :代码简单,Webservice接口使用Java的情况下建议使用。
axis代码
import java.net.MalformedURLException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import com.alibaba.fastjson.JSONObject;
public class ManageWebservice {
public static void main(String[] args) throws MalformedURLException {
Object[] obj = {"张三"};
System.out.println(myCall(obj));
}
public static String myCall(Object[] obj){
try {
String url = "http://127.0.0.1:19999/MyService.asmx?WSDL";
Service service = new Service();
String namespace = "http://tempuri.org/";
String actionUri = "getCall"; //Action路径
String op = "getCall"; //要调用的方法名
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setUseSOAPAction(true);
call.setSOAPActionURI(namespace + actionUri); // action uri
call.setOperationName(new QName(namespace, op));// 设置要调用哪个方法
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(namespace, "name"), XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置结果返回类型
String result = (String) call.invoke(obj); //方法执行后的返回值
JSONObject jo = JSONObject.parseObject(result);
System.out.println(result);
if("1".equals(jo.getString("resType"))){
return "1";
}
return "0";
} catch (Exception e) {
e.printStackTrace();
return "0";
}
}
}
xfire代码
定义接口方法,方法名需要和webservice接口定义的方法一致。
public interface MyWebservice {
// 方法名一致
public String getCall(String name);
}
实现调用
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class ManageWebservice {
public static void main(String[] args) throws MalformedURLException {
System.out.println(Call());
}
public static String Call() throws MalformedURLException{
org.codehaus.xfire.service.Service s = new ObjectServiceFactory().create(MyWebservice.class);
XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
MyWebservice my = (MyWebservice) xf.create(s, "http://127.0.0.1:8080/MyInterface/services/MyWebservice");
return my.getCall("张三");
}
}
返回值就是json字符串,具体表示什么双发协商。
(若有什么错误,请留言指正,3Q)