|
PHP代码 |
注意AppCode安全,验证API参数,处理HTTPS请求时禁用证书验证风险。 |
复 制 |
|
<?php
$host = "https://sfz2.market.alicloudapi.com";
$path = "/idcard";
$method = "GET";
$appcode = "你自己的AppCode";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "xm=%E6%9D%8E%E4%B8%89&sfz=36020319**********";
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
var_dump(curl_exec($curl));
?>
|
|
Java代码 |
注意API URL、AppCode正确性,并确保处理HTTPS请求时SSL验证配置。 |
复 制 |
|
public static void main(String[] args) {
String host = "https://sfz2.market.alicloudapi.com";
String path = "/idcard";
String method = "GET";
String appcode = "你自己的AppCode";
Map headers = new HashMap();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map querys = new HashMap();
querys.put("xm", "李三");
querys.put("sfz", "36020319**********");
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
System.out.println(response.toString());
//获取response的body
//System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
|
|
C#代码 |
注意处理HTTPS请求时的SSL证书验证,确保AppCode和URL正确性。 |
复 制 |
|
//using System.IO;
//using System.Text;
//using System.Net;
//using System.Net.Security;
//using System.Security.Cryptography.X509Certificates;
private const String host = "https://sfz2.market.alicloudapi.com";
private const String path = "/idcard";
private const String method = "GET";
private const String appcode = "你自己的AppCode";
static void Main(string[] args)
{
String querys = "xm=%E6%9D%8E%E4%B8%89&sfz=36020319**********";
String bodys = "";
String url = host + path;
HttpWebRequest httpRequest = null;
HttpWebResponse httpResponse = null;
if (0 < querys.Length)
{
url = url + "?" + querys;
}
if (host.Contains("https://"))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
}
else
{
httpRequest = (HttpWebRequest)WebRequest.Create(url);
}
httpRequest.Method = method;
httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
if (0 < bodys.Length)
{
byte[] data = Encoding.UTF8.GetBytes(bodys);
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
httpResponse = (HttpWebResponse)ex.Response;
}
Console.WriteLine(httpResponse.StatusCode);
Console.WriteLine(httpResponse.Method);
Console.WriteLine(httpResponse.Headers);
Stream st = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("\n");
}
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
|
|
Python代码 |
urllib2在Python 3中已被弃用,Python 3用户应使用urllib.request模块。 |
复 制 |
|
import urllib, urllib2, sys
import ssl
host = 'https://sfz2.market.alicloudapi.com'
path = '/idcard'
method = 'GET'
appcode = '你自己的AppCode'
querys = 'xm=%E6%9D%8E%E4%B8%89&sfz=36020319**********'
bodys = {}
url = host + path + '?' + querys
request = urllib2.Request(url)
request.add_header('Authorization', 'APPCODE ' + appcode)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urllib2.urlopen(request, context=ctx)
content = response.read()
if (content):
print(content)
|
|
ObjectC代码 |
拼接URL时,?开头查询字符串可行。建议使用NSURLComponents。缓存策略按需设,超时时间可调。 |
复 制 |
|
NSString *appcode = @"你自己的AppCode";
NSString *host = @"https://sfz2.market.alicloudapi.com";
NSString *path = @"/idcard";
NSString *method = @"GET";
NSString *querys = @"?xm=%E6%9D%8E%E4%B8%89&sfz=36020319**********";
NSString *url = [NSString stringWithFormat:@"%@%@%@", host, path , querys];
NSString *bodys = @"";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:1 timeoutInterval: 5];
request.HTTPMethod = method;
[request addValue: [NSString stringWithFormat:@"APPCODE %@" , appcode] forHTTPHeaderField: @"Authorization"];
NSURLSession *requestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDataTask *task = [requestSession dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable body , NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"Response object: %@" , response);
NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
//打印应答中的body
NSLog(@"Response body: %@" , bodyString);
}];
[task resume];
|
|
curl代码 |
|
复 制 |
|
curl -i -k --get --include 'https://sfz2.market.alicloudapi.com/idcard?xm=%E6%9D%8E%E4%B8%89&sfz=36020319**********' -H 'Authorization:APPCODE 你自己的AppCode'
|