原标题:微服务调用指南
创新应用组开发的微服务数量已经突破了百个大关,其中不光涵盖工具类基础服务,还涉猎到产品类、账户类、行情类、交易类、客户类及流程办公等基础数据存取服务。主要是在现有业务系统的基础上提供基于HTTP GET/POST的RESTful调用接口,为各业务系统的互联互通提供统一的接入标准。本文主要介绍java、java、C#和python语言来调用邮件微服务的案例,具体的接口详情还需要通过OA的微服务API文档中心查阅。
目前邮件微服务支持多附件和HTML文本内容群发,具体的参数有to、cc、bcc、subject、text、filename、file和apikey,它们分别代表了收件人、抄送人、密送人、标题、内容、附件名、附件内容和专用私钥。
Java语言
若工程有使用Maven,在POM文件中添加以下部分,当然也可以自行导入json依赖包:
org.json
json
具体代码如下:
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String args[]) {
try {
Stringresult = null;
JSONObjectobj = new JSONObject();
obj.put("to", "[email protected]");
obj.put("subject", "test");
obj.put("text", "test");
URL url =new URL("http://api.southernfund.com/mail/sendMail");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Charset", "UTF-8");
byte[] data= (obj.toString()).getBytes(Charset.forName("UTF-8"));
conn.setRequestProperty("Content-Type","application/json");
conn.connect();
OutputStreamout = conn.getOutputStream();
out.write(data);
out.flush();
out.close();
InputStreamis = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringstrRead = null;
StringBuffersbf = new StringBuffer();
while((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("rn");
}
reader.close();
result =sbf.toString();
System.out.println(result);
}catch(Exception e) {
}
}
}
Java语言
var args = {};
args['to'] =“[email protected]”;
args['subject'] = “test”;
args['text'] = “test”;
$.ajax({
type:'POST',
url:'http://api.southernfund.com/mail/sendMail',
data:args,
dataType:'json',
success:function(data){
$.each(data, function() {
alert('success');
alert(this.retmsg);
});
},
error:function(e){
alert('Error');
}
});
C#语言
string url = “http://api.southernfund.com/mail/sendMail”;
string param =
"{"to":"[email protected]","subject":"test","text":"test"}";
string callback = PostMoths(url,param);
public static string PostMoths(string url, stringparam)
{
string strURL = url;
System.Net.HttpWebRequest request;
request =(System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = param;
byte[] payload;
payload =System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response =(System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "rn";
}
return strValue;
}
Python语言
python2.X 版本代码如下:
import httplib
import json
url = 'api.southernfund.com'
conn = httplib.HTTPConnection(url)
headers = {"Content-type":"application/json"}
params =({"to":"[email protected]","subject":"test","text":"test"})
conn.request("POST","mail/sendMail", json.JSONEncoder().encode(params), headers)
response = conn.getresponse()
data = response.read()
if response.status == 200:
print'success'
printdata
else:
print'fail'
conn.close()
Python 3.X版本代码如下:
import http.client
import json
url = ''
conn = http.client.HTTPConnection(url)
headers = {"Content-type":"application/json"}
params =({"to":"[email protected]","subject":"test","text":"test"})
conn.request("POST", "http://api.southernfund.com/mail/sendMail",json.JSONEncoder().encode(params), headers)
response = conn.getresponse()
data = response.read()
if response.status == 200:
print'success'
printdata
else:
print'fail'
conn.close()
除了以上几种语言的调用方法之外,还有任何需求和建议随时欢迎联系创新应用组。返回搜狐,查看更多
责任编辑: