9 changed files with 311 additions and 13 deletions
@ -0,0 +1,106 @@ |
|||
using Newtonsoft.Json.Linq; |
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Net.Http.Headers; |
|||
using System.Text; |
|||
|
|||
namespace 齐越慧眼.Helpers |
|||
{ |
|||
public class JdApiHelper |
|||
{ |
|||
string boundary = ""; |
|||
|
|||
public string UploadFile(byte[] file, string cookie) |
|||
{ |
|||
|
|||
using (System.Net.WebClient webClient = new System.Net.WebClient()) |
|||
{ |
|||
|
|||
webClient.Headers.Add("cookie", "__jdv=76161171|direct|-|none|-|1638869805933; __jdu=16388698059328286849; areaId=19; ipLoc-djd=19-1666-0-0; PCSYCityID=CN_440000_440600_0; _pst=16927647-35691944; unick=%E7%BC%BA%E9%98%B3%E5%85%89%E7%9A%84%E5%90%8A%E5%85%B0; pin=16927647-35691944; _tp=XgOfMtPFh92ZbKpmUXyiKkww9vdhsCgPikOUZsSQog8%3D; shshshfpa=6791a0cf-5a8f-7694-426b-368379ec439f-1638869824; shshshfpb=jb35ePu5DchO3Y4rjX%20xERA%3D%3D; qrsc=3; shshshfp=1f964b3df427da1779b7c5745b5cbddb; thor=015C6984E52D7AA6F54B79351F45CECCAA86F5B71F7807178D7E5044B5B8C528095D1EC065B7BB3B3478716F0193B94DCA09CD8C24E4EF5B8FEF4E26CA166E59681381AF92538D12DD17E89019060216036A0F30231A3CA47AD2B56A508B462F2651A1976D997C9A16073A0DA758F3DDF61B41359BED5A7FC84C16677B4FE6CA75A472DB8A772628BDBCB39A9B39ADA97072F2268D249D74E955B0F17A413D89; pinId=xpLDZpChZphYLUlvWdbaKvwZj9lbjz-c; __jda=76161171.16388698059328286849.1638869806.1638869806.1638879362.2; __jdb=76161171.4.16388698059328286849|2.1638879362; __jdc=76161171; shshshsID=d20602783dcaaffcb5355ea48291319c_2_1638879385194; 3AB9D23F7A4B3C9B=RMPGIHATPH4SBVV7LECL6XAHEWL242U2UZT6MW2CFKUCKQVVMS7HKIU7GT2O63I5A3SUEKPWBSHOLYCUQ3N7HMKZ5E"); |
|||
webClient.Headers.Add("origin", "https://www.jd.com"); |
|||
webClient.Headers.Add("referer", "https://www.jd.com"); |
|||
webClient.Headers.Add("sec-fetch-dest", "iframe"); |
|||
webClient.Headers.Add("sec-fetch-mode", "navigate"); |
|||
webClient.Headers.Add("sec-fetch-site", "same-site"); |
|||
webClient.Headers.Add("sec-fetch-user", "?1"); |
|||
webClient.Headers.Add("upgrade-insecure-requests", "1"); |
|||
webClient.Headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); |
|||
webClient.Headers.Add("accept-encoding", " deflate, br"); |
|||
webClient.Headers.Add("accept-language", "zh-CN,zh;q=0.9"); |
|||
webClient.Headers.Add("cache-control", "no-cache"); |
|||
// 边界符
|
|||
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); |
|||
|
|||
webClient.Headers.Add("content-type", $"multipart/form-data; boundary={boundary}"); |
|||
|
|||
|
|||
var b = GetFileData("upload.jpg", new MemoryStream(file), boundary: boundary); |
|||
var result = webClient.UploadData("https://search.jd.com/image?op=upload", b.ToArray()); |
|||
string json = Encoding.Default.GetString(result); |
|||
|
|||
return json; |
|||
} |
|||
return string.Empty; |
|||
} |
|||
|
|||
|
|||
|
|||
public static MemoryStream GetFileData(string fileName, MemoryStream file, string fileKeyName = "file", string boundary = "") |
|||
{ |
|||
|
|||
|
|||
MemoryStream memStream = new MemoryStream(); |
|||
|
|||
// 边界符
|
|||
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); |
|||
// 最后的结束符
|
|||
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); |
|||
|
|||
|
|||
//// 写入字符串的Key
|
|||
//var stringKeyHeader = "--" + boundary +
|
|||
// "\r\nContent-Disposition: form-data; name=\"{0}\"" +
|
|||
// "\r\n\r\n{1}\r\n";
|
|||
|
|||
|
|||
// 写入文件
|
|||
const string filePartHeader = |
|||
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + |
|||
"Content-Type: image/jpeg\r\n\r\n"; |
|||
var header = string.Format(filePartHeader, fileKeyName, fileName); |
|||
var headerbytes = Encoding.UTF8.GetBytes(header); |
|||
|
|||
memStream.Write(beginBoundary, 0, beginBoundary.Length); |
|||
memStream.Write(headerbytes, 0, headerbytes.Length); |
|||
|
|||
var buffer = new byte[1024]; |
|||
int bytesRead; // =0
|
|||
file.Seek(0, SeekOrigin.Begin); |
|||
while ((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0) |
|||
{ |
|||
memStream.Write(buffer, 0, bytesRead); |
|||
} |
|||
|
|||
// 写入换行
|
|||
var contentLine = Encoding.ASCII.GetBytes("\r\n"); |
|||
memStream.Write(contentLine, 0, contentLine.Length); |
|||
|
|||
// 写入最后的结束边界符
|
|||
memStream.Write(endBoundary, 0, endBoundary.Length); |
|||
|
|||
|
|||
|
|||
memStream.Position = 0; |
|||
return memStream; |
|||
//memStream.Close();
|
|||
|
|||
|
|||
//requestStream.Close();
|
|||
|
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue