24. 外部服务支持Part 3: 直接联动¶
24.1. iFun引擎的HTTP client¶
针对须要支持在iFun引擎的认证验证或须要支持支付验证中未管理的平台,或是因除此以外的其他目的而须要通过REST方式联动外部系统的情况,iFun引擎提供了可以调用RESTful APIHttpClient。
Tip
更具体的内容请参考 API文件 。
Note
iFun引擎的HTTP client是基于 libcurl 的,所以HTTP client的错误代码请参考 libcurl错误代码 。
24.1.1. 示例: GET方法¶
24.1.1.1. 同步方式¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void Example() {
Ptr<HttpClient> http_client(new HttpClient);
if (http_client->Get("http://example.com") != CURLE_OK) {
LOG(ERROR) << "http request failure";
return;
}
const http::Response &response = http_client->response();
if (response.status_code != http::kOk) {
LOG(WARNING) << "status code: " << response.status_code;
}
LOG(INFO) << "body: " << response.body;
// 만약 Json 기반의 RESTful API 라면 아래를 참고합니다.
// Json r;
// r.FromString(response.body);
}
|
C#에서는 HttpWebRequest 클래스를 통해 HTTP 통신을 구현할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public static void Example()
{
string strUri = "http://www.example.com";
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
request.Method = "GET";
System.Net.HttpWebResponse response =
(System.Net.HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
Log.Error("http request failure");
return;
}
System.IO.Stream respStream = response.GetResponseStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(
respStream, System.Text.Encoding.Default);
string strResult = streamReader.ReadToEnd();
Log.Info ("Response: {0}", strResult);
// 만약 Json 기반의 RESTful API라면 아래를 참고합니다.
// JToken token = JToken.Parse (strResult);
// if (token is JObject) {
// JObject jsonResponse = token.ToObject<JObject>();
// Log.Info ("Response: {0}", jsonResponse.ToString());
// } else if (token is JArray) {
// JArray jsonResponse = token.ToJArray ();
// foreach(JObject obj in jsonResponse)
// {
// Log.Info ("Response: {0}", obj.ToString());
// }
// }
}
|
24.1.1.2. 非同步方式¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | void OnResponseReceived(const CURLcode code, const http::Response &response) {
if (code != CURLE_OK) {
LOG(ERROR) << "http request failure";
return;
}
if (response.status_code != http::kOk) {
LOG(WARNING) << "status code: " << response.status_code;
}
LOG(INFO) << "body: " << response.body;
// 만약 Json 기반의 RESTful API 라면 아래를 참고합니다.
// Json r;
// r.FromString(response.body);
}
void Example() {
Ptr<HttpClient> http_client(new HttpClient);
http_client->GetAsync("http://example.com", OnResponseReceived);
}
|
C#에서는 HttpWebRequest 클래스와 AsyncCallback 클래스를 통해 비동기 방식의 HTTP API 통신을 구현할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | public static void OnResponseReceived(IAsyncResult result)
{
System.Net.HttpWebResponse response =
(result.AsyncState as HttpWebRequest).EndGetResponse (result)
as HttpWebResponse;
if (response.StatusCode != HttpStatusCode.OK) {
Log.Error("http request failure");
return;
}
System.IO.Stream respStream = response.GetResponseStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(
respStream, System.Text.Encoding.Default);
string strResult = streamReader.ReadToEnd();
Log.Info ("Response: {0}", strResult);
// 만약 Json 기반의 RESTful API라면 아래를 참고합니다.
// JToken token = JToken.Parse (strResult);
// if (token is JObject) {
// JObject jsonResponse = token.ToObject<JObject>();
// Log.Info ("Response: {0}", jsonResponse.ToString());
// } else if (token is JArray) {
// JArray jsonResponse = token.ToJArray ();
// foreach(JObject obj in jsonResponse)
// {
// Log.Info ("Response: {0}", obj.ToString());
// }
// }
}
public static void Example()
{
string strUri = "http://www.example.com";
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(OnResponseReceived), request);
}
|
24.1.2. 示例: POST方法¶
24.1.2.1. 同步方式¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | void Example() {
Ptr<HttpClient> http_client(new HttpClient);
Json data;
data["example"] = "hello world!";
// http://example.com 으로 HTTP POST 요청을 보냅니다.
// Post 함수는 post data 인자로 다음과 같은 type 을 받습니다.
// - fun::Json, std::string, curl_httppost
if (http_client->Post("http://example.com", data) != CURLE_OK) {
LOG(ERROR) << "http request failure";
return;
}
const http::Response &response = http_client->response();
if (response.status_code != http::kOk) {
LOG(WARNING) << "status code: " << response.status_code;
}
LOG(INFO) << "body: " << response.body;
// 만약 Json 기반의 RESTful API 라면 아래를 참고합니다.
// Json r;
// r.FromString(response.body);
}
|
C#에서는 HttpWebRequest 클래스를 통해 HTTP 통신을 구현할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | public static void Example()
{
string strUri = "http://example.com";
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
request.Method = "POST";
JObject data = new JObject ();
data ["example"] = "hello world!";
byte[] contentBytes =
System.Text.UTF8Encoding.UTF8.GetBytes(data.ToString());
request.ContentLength = contentBytes.Length;
System.IO.Stream requestStream = request.GetRequestStream();
requestStream.Write(contentBytes, 0, contentBytes.Length);
requestStream.Close();
System.Net.HttpWebResponse response =
(System.Net.HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
Log.Warning("http request failure");
return;
}
System.IO.Stream respStream = response.GetResponseStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(respStream, System.Text.Encoding.Default);
string strResult = streamReader.ReadToEnd ();
Log.Info ("Response: {0}", strResult);
// 만약 Json 기반의 RESTful API라면 아래를 참고합니다.
// JToken token = JToken.Parse (strResult);
// if (token is JObject) {
// JObject jsonResponse = token.ToObject<JObject>();
// Log.Info ("Response: {0}", jsonResponse.ToString());
// } else if (token is JArray) {
// JArray jsonResponse = token.ToJArray ();
// foreach(JObject obj in jsonResponse)
// {
// Log.Info ("Response: {0}", obj.ToString());
// }
// }
}
|
24.1.2.2. 非同步方式¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | void OnResponseReceived(const CURLcode code, const http::Response &response) {
if (code != CURLE_OK) {
LOG(ERROR) << "http request failure";
return;
}
if (response.status_code != http::kOk) {
LOG(WARNING) << "status code: " << response.status_code;
}
LOG(INFO) << "body: " << response.body;
// 만약 Json 기반의 RESTful API 라면 아래를 참고합니다.
// Json r;
// r.FromString(response.body);
}
void Example() {
Ptr<HttpClient> http_client(new HttpClient);
Json data;
data["example"] = "hello world!";
// http://example.com 으로 HTTP POST 요청을 보냅니다.
// PostAsync 함수는 post data 인자로 다음과 같은 type 을 받습니다.
// - fun::Json, std::string, curl_httppost
http_client->PostAsync("http://example.com", data, OnResponseReceived);
}
|
C#에서는 HttpWebRequest 클래스와 AsyncCallback 클래스를 통해 비동기 방식의 HTTP API 통신을 구현할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public static void OnResponseReceived(IAsyncResult result)
{
System.Net.HttpWebResponse response =
(result.AsyncState as HttpWebRequest).EndGetResponse (result)
as HttpWebResponse;
if (response.StatusCode != HttpStatusCode.OK) {
Log.Error("http request failure");
return;
}
System.IO.Stream respStream = response.GetResponseStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(
respStream, System.Text.Encoding.Default);
string strResult = streamReader.ReadToEnd();
Log.Info ("Response: {0}", strResult);
// 만약 Json 기반의 RESTful API라면 아래를 참고합니다.
// JToken token = JToken.Parse (strResult);
// if (token is JObject) {
// JObject jsonResponse = token.ToObject<JObject>();
// Log.Info ("Response: {0}", jsonResponse.ToString());
// } else if (token is JArray) {
// JArray jsonResponse = token.ToJArray ();
// foreach(JObject obj in jsonResponse)
// {
// Log.Info ("Response: {0}", obj.ToString());
// }
// }
}
public static void Example()
{
string strUri = "http://example.com";
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
request.Method = "POST";
JObject data = new JObject ();
data ["example"] = "hello world!";
byte[] contentBytes =
System.Text.UTF8Encoding.UTF8.GetBytes(data.ToString());
request.ContentLength = contentBytes.Length;
System.IO.Stream requestStream = request.GetRequestStream();
requestStream.Write(contentBytes, 0, contentBytes.Length);
requestStream.Close();
request.BeginGetResponse(new AsyncCallback(OnResponseReceived), request);
}
|