一、前期准备工作
1.打开多多进宝,进行账号注册,打开推广者管理,新增媒体登记,选择自有平台=>网站,进行网站信息的登记,同时完成网站校验。到此,基础工作基本完成,具体详细步骤不再赘述,主要讲下接口对接以及编码,这才是让人最兴奋的地方。
二、公共部分提取
1.在进行接口对接前,先熟读下其开发文档,由文档我们知道,其接口地址、以及接口中有公共参数俩部分是雷同的,所以,我们把接口请求封装为帮助类DuoduokeRequestUntil.cs,因为其所有接口的错误返回部分一致,我们可以创建一个错误类,用于接收接口返回错误json的序列化
namespace Web.PinDuoduo.Services
{
/// <summary>
/// 请求多多客接口工具类
/// </summary>
public class DuoduokeRequestUntil
{
/// <summary>
/// get 方式 获取数据
/// </summary>
/// <param name="apiUrl"> WEBAPI地址</param>
/// <param name="parameterStr">webapi调用参数,多参数用&连接 例子参考:batchId=2023&brandId=0
/// <param name="ContentType">webapi header-ContentType形式,默认text/html;charset=UTF-8
/// </param>
public static string GetWebRequest(string apiUrl, string parameterStr, string ContentType = "text/html;charset=UTF-8")
{
string serviceAddress = apiUrl + "?" + parameterStr;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = ContentType;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
return retString;
}
/// <summary>
/// POSt方式 获取数据
/// </summary>
/// <param name="apiUrl">WEBAPI地址</param>
/// <param name="para">webapi调用参数</param>
/// <param name="ContentType">webapi header-ContentType形式,默认application/x-www-form-urlencoded;charset=utf-8
public static RequestUntilResult PostWebRequest(string apiUrl, IDictionary<string, string> para, string ContentType = "application/x-www-form-urlencoded;charset=utf-8")
{
RequestUntilResult result = new RequestUntilResult();
string serviceAddress = apiUrl;
StringBuilder buffer = new StringBuilder();//这是要提交的数据
int i = 0;
//通过泛型集合转成要提交的参数和数据
foreach (string key in para.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, para[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, para[key]);
}
i++;
}
byte[] bs = Encoding.UTF8.GetBytes(buffer.ToString());//UTF-8
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "POST";
request.ContentType = ContentType;
request.ContentLength = bs.Length;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(bs, 0, bs.Length);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
if (retString.Contains("error_response"))
{
result.IsSuccess = false;
var resultData = JObject.Parse(retString);
var resultEntity = JsonConvert.DeserializeObject<ErrorResponse>(resultData["error_response"].ToString());
result.ResultData = resultEntity;
}
else {
result.IsSuccess = true;
result.ResultData = retString;
}
return result;
}
}
/// <summary>
/// 返回错误json对象
/// </summary>
public class ErrorResponse {
public string error_msg { get; set; }
public string sub_msg { get; set; }
public string sub_code { get; set; }
public string error_code { get; set; }
public string request_id { get; set; }
}
/// <summary>
/// 请求结果class
/// </summary>
public class RequestUntilResult {
/// <summary>
/// 是否成功
/// </summary>
public bool IsSuccess { get; set; }
/// <summary>
/// 返回结果
/// 如果请求成功,返回对应的json字符串
/// 如果请求失败,则返回ErrorResponse类
/// </summary>
public object ResultData { get; set; }
}
}
2.帮助类中,我们主要定义了Get、Post俩种请求方式的帮助方法,以及返回错误类ErrorResponse
三、商品接口对接
1.拼多多提供了俩个接口进行商品查询,一个是商品搜索,传入条件参数进行商品的检索,另外一个就是商品推荐的接口
2.了解对应接口的传入参数定义,我们只需要传入对应的参数即可,所以我们首先定义一个Dictionary<string, string> param进行参数的传入,其中有一个参数为签名sign【具体解释可以看直通车】,此处直接上代码
/// <summary>
/// 获取签名字符串sign
/// https://open.pinduoduo.com/application/document/browse?idStr=8EC06C399636041E
/// </summary>
/// <param name="dicParam">参数</param>
/// <param name="client_secret"></param>
/// <returns></returns>
public static string GetSignByParam(Dictionary<string, string> dicParam, string client_secret) {
var NewDic = AsciiDictionary(dicParam);
string paramStr = client_secret;
foreach (var item in NewDic) {
paramStr += item.Key.ToString();
paramStr += item.Value.ToString();
}
paramStr += client_secret;
var sign = MD5Helper.MD5Encrypt32(paramStr);
return sign.ToUpper();
}
/// <summary>
/// 将集合key以ASCII码从小到大排序
/// </summary>
/// <param name="sArray"></param>
/// <returns></returns>
public static Dictionary<string, string> AsciiDictionary(Dictionary<string, string> sArray)
{
Dictionary<string, string> asciiDic = new Dictionary<string, string>();
string[] arrKeys = sArray.Keys.ToArray();
Array.Sort(arrKeys, string.CompareOrdinal);
foreach (var key in arrKeys)
{
string value = sArray[key];
asciiDic.Add(key, value);
}
return asciiDic;
}
3.调用上述方法, 我们就可以直接得到对应的sign签名,最终调用DuoduokeRequestUntil.PostWebRequest方法,传入对应的地址,以及参数,我们就可以得到对应的数据
4.另外一个接口也是如此,最终效果看:http://51softwarebox.com/duoduojuan