背景
在1688有個功能,就是上傳圖片,就能夠找到相似的商品。以下web
網址 :https://www.1688.com/json
這時候,咱們可使用程序來代替,大批量的完成圖片上傳功能。數組
實現思路
一、找到圖片上傳接口
post請求,form表單中有signature簽名
微信
二、再找sign生成接口,全局搜素找一下signature,發現了一個返回signature的接口。
接口連接:https://open-s.1688.com/openservice/ossDataService
這個接口也有一個變更的參數 appKey
全局搜索後在js文件中查看一下
往下找就能夠發現appkey的生成了。
經過debug來查看生成規則。
獲取加密時間的接口:https://open-s.1688.com/openservice/.htm?
參數:outfmt =json&serviceIds=cbu.searchweb.config.system.currenttimecookie
須要先請求這個接口,獲取加密時間。app
private void go(string parhfile) { var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/.htm?serviceIds=cbu.searchweb.config.system.currenttime&outfmt=json", 5000, null, null); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response.StatusCode == HttpStatusCode.OK) { var ss = JsonConvert.DeserializeObject<Currenttimemodel>(retString.Replace("cbu.searchweb.config.system.currenttime", "currenttime")); // SetText("加密時間:" + ss.currenttime.dataSet.ToString()); getsin(ss.currenttime.dataSet.ToString(), parhfile); } else { SetText("錯誤代碼:" + response.StatusCode.ToString()); } }
再把時間和appName 傳入 getAppKey。
而後e= 「appname ; t」 ,appName的base64編碼以後的結果是 「cGNfdHVzb3U=」
curl
通過encode64返回 i ( appkey)編輯器
而後經過上面生成sign的接口:https://open-s.1688.com/openservice/ossDataServicepost
傳入參數就行請求,就能夠返回signature,policy,accessid。ui
1
2
3
4
"appName": key,
"appKey": base64.b64encode(appkey.encode("utf-8")),
}
/// <summary> /// sign生成接口 /// </summary> /// <param name="dataSet"></param> private void getsin(string dataSet, string parhfile) { string appName = "pc_tusou"; //getAppKey string appKey = Base64.EncodeBase64("utf-8", appName + ";" + dataSet.ToString()); var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/ossDataService?appName=" + appName + "&appKey=" + appKey, 5000, null, null); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response.StatusCode == HttpStatusCode.OK) { var ss = JsonConvert.DeserializeObject<Rootsin>(retString); // SetText("sign:" + ss.data.signature); string key = "cbuimgsearch/" + Base64.Getimgname() + Base64.GetTimeStamp() + ".jepg"; var client = new RestClient("https://cbusearch.oss-cn-shanghai.aliyuncs.com/"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Origin", "https://www.1688.com"); client.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"; request.AddHeader("Accept", "*/*"); request.AddHeader("Cache-Control", "no-cache"); request.AddHeader("cookie", "_samesite_flag_=true; _tb_token_=ee5138b911917; cookie2=163f6e3722351213514df4c9ab9116f6; t=96e8d0ab6d636f19306c429b276db552; __cn_logon__=false; ali_ab=120.253.224.246.1587973275662.6; l=caJGIJNTkgnFkWiGkSYyeKDwPQuOAiFJdcPgDahIhDlFGpKMvULclIQGPBDmDhmDdCsLYIU; na=ijBRbdRXZeKwRcTHilfNHSt+; "); request.AddHeader("refer", "https://www.1688.com/"); request.AddParameter("name", Base64.Getname() + ".jpeg"); request.AddParameter("key", key); request.AddParameter("OSSAccessKeyId", ss.data.accessid); request.AddParameter("callback", ""); request.AddParameter("policy", ss.data.policy); request.AddParameter("signature", ss.data.signature); request.AddParameter("success_action_status", "200"); request.AddFile("file", parhfile); IRestResponse response2 = client.Execute(request); Console.WriteLine(response2.Content); if (response2.StatusCode == HttpStatusCode.OK) { string picurl = "https://s.1688.com/youyuan/index.htm?tab=imageSearch&imageType=oss&imageAddress=" + key + "&spm="; SetText3("\r\n" + picurl); Write(picurl); } else { SetText("錯誤代碼:" + response2.StatusCode.ToString()); } } else { SetText("錯誤代碼:" + response.StatusCode.ToString()); } }
三、數據詳情接口
圖片上傳以後,返回的數據接口:
https://search.1688.com/service/imageSearchOfferResultViewService?
參數:
imageAddress是在上傳圖片以後返回的值
requestId 初始化參數,能夠爲空。
整個流程就是這樣了,接着構造請求就能夠獲取數據了。
完整代碼
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Net.Http.Headers;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;using Newtonsoft.Json;using Reptiles1688;using RestSharp;
namespace WindowsFormsApp1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(new WaitCallback(crawlingWeb), "test"); }
public void Write(string s) { string path = System.Environment.CurrentDirectory + "\\圖片url\\" + Guid.NewGuid().ToString(); if (!Directory.Exists(path)) Directory.CreateDirectory(path); FileStream fs = new FileStream(path + "\\data.txt", FileMode.Create); //得到字節數組 byte[] data = System.Text.Encoding.Default.GetBytes(s); //開始寫入 fs.Write(data, 0, data.Length); //清空緩衝區、關閉流 fs.Flush(); fs.Close(); }
private delegate void SetLabelDelegate(string value);
private void SetText(string value) { if (this.InvokeRequired) { SetLabelDelegate d = new SetLabelDelegate(SetText); this.Invoke(d, new object[] { value }); } else { textBox1.Text = value.ToString() + textBox1.Text; } }
private delegate void SetLabelDelegate3(string value);
private void SetText3(string value) { if (this.InvokeRequired) { SetLabelDelegate3 d = new SetLabelDelegate3(SetText3); this.Invoke(d, new object[] { value }); } else { textBox3.Text = value.ToString() + textBox3.Text; } }
private void crawlingWeb(object data) { for (int aa = 1; aa < 50; aa++) { // SetText3(aa.ToString()); for (int a = 1; a < 4; a++) { string ss = "D:\\pppppppppppppp\\" + a + ".jpg"; // SetText3(aa.ToString() + "-" + a);
go(ss); Thread.Sleep(500); } }
SetText3("\r\n ok"); }
private void go(string parhfile) { var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/.htm?serviceIds=cbu.searchweb.config.system.currenttime&outfmt=json", 5000, null, null); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response.StatusCode == HttpStatusCode.OK) { var ss = JsonConvert.DeserializeObject<Currenttimemodel>(retString.Replace("cbu.searchweb.config.system.currenttime", "currenttime")); // SetText("加密時間:" + ss.currenttime.dataSet.ToString()); getsin(ss.currenttime.dataSet.ToString(), parhfile); } else { SetText("錯誤代碼:" + response.StatusCode.ToString()); } }
/// <summary> /// sign生成接口 /// </summary> /// <param name="dataSet"></param> private void getsin(string dataSet, string parhfile) { string appName = "pc_tusou"; //getAppKey string appKey = Base64.EncodeBase64("utf-8", appName + ";" + dataSet.ToString()); var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/ossDataService?appName=" + appName + "&appKey=" + appKey, 5000, null, null); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response.StatusCode == HttpStatusCode.OK) { var ss = JsonConvert.DeserializeObject<Rootsin>(retString); // SetText("sign:" + ss.data.signature); string key = "cbuimgsearch/" + Base64.Getimgname() + Base64.GetTimeStamp() + ".jepg"; var client = new RestClient("https://cbusearch.oss-cn-shanghai.aliyuncs.com/"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Origin", "https://www.1688.com"); client.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"; request.AddHeader("Accept", "*/*"); request.AddHeader("Cache-Control", "no-cache"); request.AddHeader("cookie", "_samesite_flag_=true; _tb_token_=ee5138b911917; cookie2=163f6e3722351213514df4c9ab9116f6; t=96e8d0ab6d636f19306c429b276db552; __cn_logon__=false; ali_ab=120.253.224.246.1587973275662.6; l=caJGIJNTkgnFkWiGkSYyeKDwPQuOAiFJdcPgDahIhDlFGpKMvULclIQGPBDmDhmDdCsLYIU; na=ijBRbdRXZeKwRcTHilfNHSt+; "); request.AddHeader("refer", "https://www.1688.com/"); request.AddParameter("name", Base64.Getname() + ".jpeg"); request.AddParameter("key", key); request.AddParameter("OSSAccessKeyId", ss.data.accessid); request.AddParameter("callback", ""); request.AddParameter("policy", ss.data.policy); request.AddParameter("signature", ss.data.signature); request.AddParameter("success_action_status", "200"); request.AddFile("file", parhfile); IRestResponse response2 = client.Execute(request); Console.WriteLine(response2.Content); if (response2.StatusCode == HttpStatusCode.OK) { string picurl = "https://s.1688.com/youyuan/index.htm?tab=imageSearch&imageType=oss&imageAddress=" + key + "&spm="; SetText3("\r\n" + picurl); Write(picurl); } else { SetText("錯誤代碼:" + response2.StatusCode.ToString()); } } else { SetText("錯誤代碼:" + response.StatusCode.ToString()); } } }}
本文分享自微信公衆號 - dotNET跨平臺(opendotnet)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。