9.6.3.6. getUrlRespHtml用法示例:POST操作

在模拟登陆时,往往会用到POST,会传递对应的POST数据

此处,主要有两种方式传递POST数据:

  • postDict

    一般都是通过postDict传递数据进去

    然后内部通过quoteParas转换为对应的post data,是以"&"为分隔符的。

  • postDataStr

    个别情况下,特殊的情况下,会用到此postDataStr

    其传递的post数据,是以换行为分隔符的。此时需要,不设置postDict(默认为null),然后设置对应的postDataStr即可。

下面,针对两种情况,都给出对应的多个示例来说明如何使用:

9.6.3.6.1. postDict示例:getDomainPageRank

比如,之前折腾:第 9.11 节 “查找获得域名的Page Rank:getDomainPageRank”时所用到的:


    //Method 1: use http://www.pagerankme.com/
    queryUrl = "http://www.pagerankme.com/";
    postDict = new Dictionary<string, string>();
    postDict.Add("url", domainUrl);
    respHtml = getUrlRespHtml(queryUrl, postDict: postDict);

                
9.6.3.6.2. postDict示例:downloadSongtasteMusic

比如,之前折腾:DownloadSongtasteMusic时所用到的:


    const string stHtmlCharset = "GB18030";

    Dictionary<string, string> headerDict = new Dictionary<string, string>();
    headerDict.Add("x-requested-with", "XMLHttpRequest");
    // when click play
    // access http://songtaste.com/time.php, post data:
    //str=5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51&sid=3015123&t=0
    Dictionary<string, string> postDict = new Dictionary<string, string>();
    postDict.Add("str", str);
    postDict.Add("sid", sid);
    postDict.Add("t", "0");
    string getRealAddrUrl = "http://songtaste.com/time.php";
    songInfo.realAddr = crl.getUrlRespHtml(getRealAddrUrl, headerDict:headerDict, postDict:postDict, charset:stHtmlCharset);

                
9.6.3.6.3. postDataStr示例:百度API上传文件

比如,之前折腾:【未解决】通过百度API上传单个文件出现403的错误时所遇到的就是,post数据是以换行符非分隔符的,所以就要去直接设置对应的postDataStr:


string[] token = respTokenJson.Split(',');
 
string tokenStr = token[2].Split(':')[1].Trim('"');
 
byte[] fileBytes = null;
string filename = "fileForUpload2.txt";
string fullFilePath = @"d:\" + filename;
using (FileStream fs = new FileStream(fullFilePath, FileMode.Open))
{
    fileBytes = new byte[fs.Length];
    fs.Read(fileBytes, 0, fileBytes.Length);
}
 
StringBuilder buffer = new StringBuilder();
char[] fileCh = new char[fileBytes.Length];
for (int i = 0; i < fileBytes.Length; i++)
    fileCh[i] = (char)fileBytes[i];
 
buffer.Append(fileCh);
//postDict = new Dictionary<string, string>();
//postDict.Add("file", buffer.ToString());
 
string postDataStr = buffer.ToString();
 
string uploadSingleFileUrl = "https://pcs.baidu.com/rest/2.0/pcs/file?";
Dictionary<string, string> queryParaDict = new Dictionary<string, string>();
queryParaDict.Add("method", "upload");
queryParaDict.Add("access_token", tokenStr);
queryParaDict.Add("path", "/apps/测试应用/" + filename);
uploadSingleFileUrl += crifanLib.quoteParas(queryParaDict);
 
curCookies = crifanLib.getCurCookies();
newCookies = new CookieCollection();
foreach (Cookie ck in curCookies)
{
    if (ck.Name == "BAIDUID" || ck.Name == "BDUSS")
    {
        ck.Domain = "pcs.baidu.com";
    }
 
    newCookies.Add(ck);
}
crifanLib.setCurCookies(newCookies);
 
string boundaryValue = "----WebKitFormBoundaryS0JIa4uHF7yHd8xJ";
string boundaryExpression = "boundary=" + boundaryValue;
 
headerDict = new Dictionary<string, string>();
headerDict.Add("Pragma", "no-cache");
headerDict.Add("Content-Type", "multipart/form-data;" + " " + boundaryExpression);
 
postDataStr = boundaryValue + "\r\n"
            + "Content-Disposition: form-data; name=\"file\"" + "\r\n"
            + postDataStr + "\r\n"
            + boundaryValue;
 
//string str = crifanLib.getUrlRespHtml(
//    string.Format(@"https://pcs.baidu.com/rest/2.0/pcs/file?method=upload&path=%2Fapps%2F%E6%B5%8B%E8%AF%95%E5%BA%94%E7%94%A8%2F78.jpg&access_token={0}", tokenStr),
//    headerDict, postDict);
string respJson = crifanLib.getUrlRespHtml(uploadSingleFileUrl, headerDict:headerDict, postDataStr: postDataStr);

                
9.6.3.6.4. postDataStr示例:网易的心情随笔

比如,之前折腾:【记录】给BlogsToWordPress添加支持导出网易的心情随笔时所遇到的就是,post数据是以换行符非分隔符的,所以就要去直接设置对应的postDataStr:


    string postDataStr =
        "callCount=1" + "\r\n" +
        "scriptSessionId=${scriptSessionId}187" + "\r\n" +
        "c0-scriptName=BlogBeanNew" + "\r\n" +
        "c0-methodName=getBlogs" + "\r\n" +
        "c0-id=0" + "\r\n" +
        "c0-param0=" + "number:" + userId + "\r\n" +
        "c0-param1=" + "number:" + startBlogIdx + "\r\n" +
        "c0-param2=" + "number:" + onceGetNum;

    //http://api.blog.163.com/ni_chen/dwr/call/plaincall/BlogBeanNew.getBlogs.dwr
    string getBlogsDwrMainUrl = blogApi163 + "/" + blogUser + "/" + "dwr/call/plaincall/BlogBeanNew.getBlogs.dwr";
         
    Dictionary<string, string> headerDict = new Dictionary<string, string>();
    headerDict = new Dictionary<string, string>();
    //Referer    http://api.blog.163.com/crossdomain.html?t=20100205
    headerDict.Add("Referer", "http://api.blog.163.com/crossdomain.html?t=20100205");
    headerDict.Add("Content-Type", "text/plain");
    
    string blogsRespHtml = getUrlRespHtml(getBlogsDwrMainUrl, headerDict:headerDict, postDataStr:postDataStr);