Try to publish the product and upload the picture via HttpWebRequest to Taobao

Source: Internet
Author: User
Keywords HTTP merchandise this token through

Friends opened a Taobao shop, so often to the new products and pictures posted to the shop, and sometimes also need to upload a lot of merchandise. If it's too time-consuming to upload a piece of merchandise, I wonder if I can use WinForm to write a program to send Post/get requests through WebRequest. Because the upload top grade when there is a common form field information needs to be submitted, also need to upload pictures, so in writing HttpWebRequest reference this post http://bytes.com/topic/c-sharp/answers/268661- How-upload-file-via-c-code (how upload file via C # code?)

Taobao published a product generally divided into two steps (that is, two pages, are actually submitted to the same URL for processing: http://sell.taobao.com//auction/publish/ publish.htm), the first step is to select the classification of goods, the second step is to fill in the product information and related photos.

The raw stream of the HTTP request after the second step submitted by the Ieinspector software is roughly:

post/auction/publish/publish.htm http/1.1


accept:image/gif, JPEG, Image/pjpeg, Image/pjpeg, Application/x-shockwave-flash, application/ X-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml, application/ X-silverlight, Application/vnd.ms-excel, Application/msword, Application/vnd.ms-powerpoint, */*


referer:http://sell.taobao.com/auction/publish/publish.htm


ACCEPT-LANGUAGE:ZH-CN


user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; trident/4.0. NET CLR 2.0.50727;. NET CLR 3.0.04506.648;. NET CLR 3.5.21022)


Content-type:multipart/form-data; boundary=---------------------------7d9da39c0084


Accept-encoding:gzip, deflate


Host:sell.taobao.com


content-length:217424


connection:keep-alive


Cache-control:no-cache


cookie:po=50008882_100.0_14_0.0_%e5%8c%. selltbtoken=%2ceee15ee6bbaee%237856351072575161%2ce87e3fe110be1%237856331541398694





-----------------------------7d9da39c0084


Content-disposition:form-data; Name= "Action"





publish/publishaction


-----------------------------7d9da39c0084


Content-disposition:form-data; Name= "Ismimageuser"





-----------------------------7d9da39c0084


Content-disposition:form-data; Name= "_fma.pu._0.ca"





50008882


-----------------------------7d9da39c0084


Content-disposition:form-data; Name= "_FMA.PU._0.I"





ndczyze0mji3yzc4ztczogi2zgq3zjjiythjmgzkywegyjc2mgm1mdbjywvjymfln2i0mzrlzjqwztizogi0nwigmti1ntc1mda0njqwna= =


-----------------------------7d9da39c0084





-----------------------------7d9da39c0084


Content-disposition:form-data; Name= "_fma.pu._0.ima"; Filename= "Dsc_6873.jpg"


Content-type:image/pjpeg





.....

So before making this HTTP request, we need to prepare a couple of things, namely the cookie value, the _FMA.PU._0.I value, and the Selltbtoken value.

The best way to get a cookie value is to use the Webhttprequest analog logon process to send an HTTP request containing a username and password to the server. After the server login successful HttpResponse contains the Set-cookie part of the cookie we are looking for. Because now Taobao login page to enter the password part of the plug-in mode, as if the request before the plug-in will generate a token and so on, so simple webhttprequest send login request can not achieve our purpose, as for his login security encryption process is how the specific thing, I didn't have enough time to find out, so I gave up the way to get cookies. Finally simply use the most original method, namely in the Web page login taobao, and then use Ieinspector to track the returned packets, the cookie part of the copy out of the direct use of it.

Next is to get the value of _frma.pu._0.i, in fact, what this value is used I do not know. By observation, it is found that the first occurrence of this value is a GET request urlhttp://sell.taobao.com//auction/publish/publish.htm?auction_type=b&auth_alert= True, the returned HTML text appears as a hidden input form. So I simulate the process in the code and parse out this value:

private String getfmapu_0i ()


{


string actionurl = "Http://sell.taobao.com//auction/publish/publish.htm?auction_type=b&auth_alert=true";


HttpWebRequest HttpWebRequest = WebRequest.Create (ActionURL) as HttpWebRequest;


Httpwebrequest.method = "get";


httpwebrequest.headers["Cookie" = Txtcookie.text;





WebResponse WebResponse = HttpWebRequest.GetResponse ();


Stream stream = WebResponse.GetResponseStream ();


StreamReader rd = new StreamReader (Stream, encoding.getencoding ("gb2312"));


string oupttext = Rd. ReadToEnd ();





Rd. Close ();


WebResponse.Close ();





string startstr = "<input type=\" hidden\ "name=\" _fma.pu._0.i\ "value=\" ";


int startpos = Oupttext.indexof (startstr) + startstr.length;


int endpos = oupttext.indexof ("\/>", startpos);


return oupttext.substring (Startpos, Endpos-startpos);


}

I do not understand the meaning of Selltbtoken value, just look at the name seems to be a token. Although I also found a selltbtoken in the cookie, but their values are not the same, I did not find the connection between them, for example, the Selltbtoken value in the cookie is%2c33d58361b5b7% 2312446852732896952%2ce337766ee471f%2312317842106832544%2cebd13373eed76%2312317854579307544%2cfe3e333de58e6% 2313206186303479904%efd65353b8377%239822969650842632%2cb5b5e7bb31ae%2312957211756764136%2ce7e883bdd77e3% 237273521182867360%2ce8b1374b13bee%2313038885937950800%2c5780d773e5075%2312322949035362088%2c715735e53ed3b% 237439832831399704, and the Selltbtoken value in post data in the second step request is 5be7931b93e57#9260334924738040. The value of the Selltbtoken we are looking for is in the hidden input of the Product trust Information page, so it is still the HttpWebRequest that sends the POST request and parses the HTML to get the value:

private String Getselltbtoken (string fmapu_0i)


{


string actionurl = "http://sell.taobao.com/auction/publish/publish.htm";





NameValueCollection paras = new NameValueCollection ();


paras. ADD ("_fma.pu._0.ca", "50008882");


paras. ADD ("_fma.pu._0.cat", "20000%3a29973");


paras. ADD ("Action", "Publish%2fpublishaction");


paras. ADD ("Event_submit_do_input_auction_info", "1");


paras. ADD ("_fma.pu._0.i", fmapu_0i);


paras. ADD ("_fma.pu._0.x", "");


paras. ADD ("_fma.pu._0.a", "B");


paras. ADD ("_fma.pu._0.t", "");


paras. ADD ("_fma.pu._0.pi", "");


paras. ADD ("Isedit", "");


paras. ADD ("Ishaveimageextra", "");


paras. ADD ("_fma.pu._0.cat", "20000%3a29973");


paras. ADD ("_fma.pu._0.d", "");


paras. ADD ("_fma.pu._0.q", "10");


paras. ADD ("_fma.pu._0.du", "14");


paras. ADD ("_fma.pu._0.stu", "5");


paras. ADD ("_fma.pu._0.m", "");


paras. ADD ("_fma.pu._0.bu", "0");


paras. ADD ("_fma.pu._0.in", "0.0");


paras. ADD ("_fma.pu._0.pro", "%b1%b1%be%a9");


paras. ADD ("_fma.pu._0.c", "%b1%b1%be%a9");


paras. ADD ("_fma.pu._0.sh", "1");


paras. ADD ("_fma.pu._0.se", "0.0");


paras. ADD ("_fma.pu._0.sec", "0.0");


paras. ADD ("_fma.pu._0.secu", "0.0");


paras. ADD ("_FMA.PU._0.PA", "%bf%ee%b5%bd%b7%a2%bb%f5");


paras. ADD ("_fma.pu._0.h", "0");


paras. ADD ("_fma.pu._0.ha", "0");


paras. ADD ("_fma.pu._0.secur", "1");


paras. ADD ("_FMA.PU._0.R", "A");


paras. ADD ("_fma.pu._0.auc", "0");


paras. ADD ("_fma.pu._0.st", "");


paras. ADD ("_fma.pu._0.sto", "");


paras. ADD ("_FMA.PU._0.E", "false");


paras. ADD ("_fma.pu._0.fr", "");


paras. ADD ("_fma.pu._0.sho", "%2c143119754%2c");


paras. ADD ("_fma.pu._0.aut", "false");


paras. ADD ("_fma.pu._0.b", "100.0");


paras. ADD ("Olddesc", "");


paras. ADD ("Oldstory", "");


paras. ADD ("Productrootcat", "");


paras. ADD ("ProductId", "");


paras. ADD ("Oldcat", "");


paras. ADD ("Oldspuid", "");


paras. ADD ("Oldcategoryproperty", "");


paras. ADD ("_fma.pu._0.isa", "false");


paras. ADD ("_fma.pu._0.po", "2047393");


paras. ADD ("_fma.pu._0.an", "false");


paras. ADD ("_FMA.PU._0.O", "");


paras. ADD ("_fma.pu._0.auct", "0.0");


paras. ADD ("_FMA.PU._0.N", "");


paras. ADD ("_fma.pu._0.isn", "false");


paras. ADD ("_fma.pu._0.iso", "false");


paras. ADD ("_fma.pu._0.s", "");


paras. ADD ("_fma.pu._0.sk", "");


paras. ADD ("_fma.pu._0.gtr", "");


paras. ADD ("_FMA.PU._0.G", "");





StringBuilder sbdata = new StringBuilder ();


foreach (string key in paras. Keys)


{


if (sbdata.length > 0)


sbdata.append ("&");


Sbdata.append (String.Format ("{0}={1}", Key,paras[key));


}


Sbdata.insert (0, "\ r \ n");





HttpWebRequest HttpWebRequest2 = (HttpWebRequest) webrequest.create (ActionURL);


Httpwebrequest2.contenttype = "application/x-www-form-urlencoded";


Httpwebrequest2.method = "POST";


httpwebrequest2.keepalive = true;


httpwebrequest2.credentials = System.Net.CredentialCache.DefaultCredentials;


httpwebrequest2.headers["Cookie" = Txtcookie.text;





Stream Memstream =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.