二维码编解码
支持二维码的生成与识别。二维码生成时,直接返回生成的二维码图片
该产品拥有以下APIs:
-
二维码生成 复制代码
-
二维码解码 复制代码
注意,该示例代码仅适用于 www.apishop.net网站下API 使用该产品前,您需要通过 https://www.apishop.net/#/api/detail/?productID=128申请API服务
1、二维码生成
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace apishop_sdk
{
class Program
{
/**
* 转发请求到目的主机
* @param method string 请求方法
* @param url string 请求地址
* @param params Dictionary<string,string> 请求参数
* @param headers Dictionary<string,string> 请求头
* @return string
**/
static string apishop_send_request(string method, string url, Dictionary<string, string> param, Dictionary<string, string> headers)
{
string result = string.Empty;
try
{
string paramData = "";
if (param != && param.Count > 0)
{
StringBuilder sbuilder = new StringBuilder();
foreach (var item in param)
{
if (sbuilder.Length > 0)
{
sbuilder.Append("&");
}
sbuilder.Append(item.Key + "=" + item.Value);
}
paramData = sbuilder.ToString();
}
method = method.ToUpper();
if (method == "GET")
{
url = string.Format("{0}?{1}", url, paramData);
}
HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
if (method == "GET")
{
wbRequest.Method = "GET";
}
else if (method == "POST")
{
wbRequest.Method = "POST";
wbRequest.ContentType = "application/x-www-form-urlencoded";
wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
using (Stream requestStream = wbRequest.GetRequestStream())
{
using (StreamWriter swrite = new StreamWriter(requestStream))
{
swrite.Write(paramData);
}
}
}
HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
using (Stream responseStream = wbResponse.GetResponseStream())
{
using (StreamReader sread = new StreamReader(responseStream))
{
result = sread.ReadToEnd();
}
}
}
catch
{
return "";
}
return result;
}
class Response
{
public string statusCode;
}
static void Main(string[] args)
{
string method = "POST";
string url = "https://api.apishop.net/common/qrcode/content2QrCode";
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("content", ""); //内容
param.Add("size", ""); //图片大小(范围:1~10),默认为4【注:手机扫描,size为4即可】
param.Add("level", ""); //容错(0:L,1:M,:2:H,3:Q),默认为0
param.Add("margin", ""); //边框大小(范围:1~10),默认为3
Dictionary<string, string> headers = ;
string result = apishop_send_request(method, url, param, headers);
if (result == "")
{
//返回内容异常,发送请求失败
Console.WriteLine("发送请求失败");
return;
}
Response res = new JavaScriptSerializer().Deserialize<Response>(result);
if (res.statusCode == "000000")
{
//状态码为000000, 说明请求成功
Console.WriteLine(string.Format("请求成功: {0}", result));
}
else
{
//状态码非000000, 说明请求失败
Console.WriteLine(string.Format("请求失败: {0}", result));
}
Console.ReadLine();
}
}
}
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace apishop_sdk
{
class Program
{
/**
* 转发请求到目的主机
* @param method string 请求方法
* @param url string 请求地址
* @param params Dictionary<string,string> 请求参数
* @param headers Dictionary<string,string> 请求头
* @return string
**/
static string apishop_send_request(string method, string url, Dictionary<string, string> param, Dictionary<string, string> headers)
{
string result = string.Empty;
try
{
string paramData = "";
if (param != && param.Count > 0)
{
StringBuilder sbuilder = new StringBuilder();
foreach (var item in param)
{
if (sbuilder.Length > 0)
{
sbuilder.Append("&");
}
sbuilder.Append(item.Key + "=" + item.Value);
}
paramData = sbuilder.ToString();
}
method = method.ToUpper();
if (method == "GET")
{
url = string.Format("{0}?{1}", url, paramData);
}
HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
if (method == "GET")
{
wbRequest.Method = "GET";
}
else if (method == "POST")
{
wbRequest.Method = "POST";
wbRequest.ContentType = "application/x-www-form-urlencoded";
wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
using (Stream requestStream = wbRequest.GetRequestStream())
{
using (StreamWriter swrite = new StreamWriter(requestStream))
{
swrite.Write(paramData);
}
}
}
HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
using (Stream responseStream = wbResponse.GetResponseStream())
{
using (StreamReader sread = new StreamReader(responseStream))
{
result = sread.ReadToEnd();
}
}
}
catch
{
return "";
}
return result;
}
class Response
{
public string statusCode;
}
static void Main(string[] args)
{
string method = "POST";
string url = "https://api.apishop.net/common/qrcode/content2QrCode";
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("content", ""); //内容
param.Add("size", ""); //图片大小(范围:1~10),默认为4【注:手机扫描,size为4即可】
param.Add("level", ""); //容错(0:L,1:M,:2:H,3:Q),默认为0
param.Add("margin", ""); //边框大小(范围:1~10),默认为3
Dictionary<string, string> headers = ;
string result = apishop_send_request(method, url, param, headers);
if (result == "")
{
//返回内容异常,发送请求失败
Console.WriteLine("发送请求失败");
return;
}
Response res = new JavaScriptSerializer().Deserialize<Response>(result);
if (res.statusCode == "000000")
{
//状态码为000000, 说明请求成功
Console.WriteLine(string.Format("请求成功: {0}", result));
}
else
{
//状态码非000000, 说明请求失败
Console.WriteLine(string.Format("请求失败: {0}", result));
}
Console.ReadLine();
}
}
}
复制代码
2、二维码解码
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace apishop_sdk
{
class Program
{
/**
* 转发请求到目的主机
* @param method string 请求方法
* @param url string 请求地址
* @param params Dictionary<string,string> 请求参数
* @param headers Dictionary<string,string> 请求头
* @return string
**/
static string apishop_send_request(string method, string url, Dictionary<string, string> param, Dictionary<string, string> headers)
{
string result = string.Empty;
try
{
string paramData = "";
if (param != && param.Count > 0)
{
StringBuilder sbuilder = new StringBuilder();
foreach (var item in param)
{
if (sbuilder.Length > 0)
{
sbuilder.Append("&");
}
sbuilder.Append(item.Key + "=" + item.Value);
}
paramData = sbuilder.ToString();
}
method = method.ToUpper();
if (method == "GET")
{
url = string.Format("{0}?{1}", url, paramData);
}
HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
if (method == "GET")
{
wbRequest.Method = "GET";
}
else if (method == "POST")
{
wbRequest.Method = "POST";
wbRequest.ContentType = "application/x-www-form-urlencoded";
wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
using (Stream requestStream = wbRequest.GetRequestStream())
{
using (StreamWriter swrite = new StreamWriter(requestStream))
{
swrite.Write(paramData);
}
}
}
HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
using (Stream responseStream = wbResponse.GetResponseStream())
{
using (StreamReader sread = new StreamReader(responseStream))
{
result = sread.ReadToEnd();
}
}
}
catch
{
return "";
}
return result;
}
class Response
{
public string statusCode;
}
static void Main(string[] args)
{
string method = "POST";
string url = "https://api.apishop.net/common/qrcode/qrCode2Content";
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("image", ""); //二维码图片文件
Dictionary<string, string> headers = ;
string result = apishop_send_request(method, url, param, headers);
if (result == "")
{
//返回内容异常,发送请求失败
Console.WriteLine("发送请求失败");
return;
}
Response res = new JavaScriptSerializer().Deserialize<Response>(result);
if (res.statusCode == "000000")
{
//状态码为000000, 说明请求成功
Console.WriteLine(string.Format("请求成功: {0}", result));
}
else
{
//状态码非000000, 说明请求失败
Console.WriteLine(string.Format("请求失败: {0}", result));
}
Console.ReadLine();
}
}
}
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace apishop_sdk
{
class Program
{
/**
* 转发请求到目的主机
* @param method string 请求方法
* @param url string 请求地址
* @param params Dictionary<string,string> 请求参数
* @param headers Dictionary<string,string> 请求头
* @return string
**/
static string apishop_send_request(string method, string url, Dictionary<string, string> param, Dictionary<string, string> headers)
{
string result = string.Empty;
try
{
string paramData = "";
if (param != && param.Count > 0)
{
StringBuilder sbuilder = new StringBuilder();
foreach (var item in param)
{
if (sbuilder.Length > 0)
{
sbuilder.Append("&");
}
sbuilder.Append(item.Key + "=" + item.Value);
}
paramData = sbuilder.ToString();
}
method = method.ToUpper();
if (method == "GET")
{
url = string.Format("{0}?{1}", url, paramData);
}
HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
if (method == "GET")
{
wbRequest.Method = "GET";
}
else if (method == "POST")
{
wbRequest.Method = "POST";
wbRequest.ContentType = "application/x-www-form-urlencoded";
wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
using (Stream requestStream = wbRequest.GetRequestStream())
{
using (StreamWriter swrite = new StreamWriter(requestStream))
{
swrite.Write(paramData);
}
}
}
HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
using (Stream responseStream = wbResponse.GetResponseStream())
{
using (StreamReader sread = new StreamReader(responseStream))
{
result = sread.ReadToEnd();
}
}
}
catch
{
return "";
}
return result;
}
class Response
{
public string statusCode;
}
static void Main(string[] args)
{
string method = "POST";
string url = "https://api.apishop.net/common/qrcode/qrCode2Content";
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("image", ""); //二维码图片文件
Dictionary<string, string> headers = ;
string result = apishop_send_request(method, url, param, headers);
if (result == "")
{
//返回内容异常,发送请求失败
Console.WriteLine("发送请求失败");
return;
}
Response res = new JavaScriptSerializer().Deserialize<Response>(result);
if (res.statusCode == "000000")
{
//状态码为000000, 说明请求成功
Console.WriteLine(string.Format("请求成功: {0}", result));
}
else
{
//状态码非000000, 说明请求失败
Console.WriteLine(string.Format("请求失败: {0}", result));
}
Console.ReadLine();
}
}
}
复制代码