asp.net c# 網站程式評論同步到多說平臺

龐順龍發表於2019-05-11

asp.net c# 網站程式評論同步到多說平臺

1、首先參照官網api,測試api介面http://dev.duoshuo.com/docs/513fbb89a1e7a29b33000064

2、引數說明就不做解釋了,可以使用本站web工具進行post測試http://www.80iter.com/WebTools/httpGetPost/httpGetPost.html



結果提交成功了,各位可看下http://www.80iter.com/blog/1473148444903609 這篇文章底部就有了測試的這條評論

3、經過2步,剩下的就簡單了,本站基於net開發,所以寫一個頁面,點選獲取本站所有需要同步到多說的評論集合資訊,遍歷post到多說的這個api介面即可。

4、程式碼展示


/// <summary>
/// Http (GET/POST)
/// </summary>
/// <param name="url">請求URL</param>
/// <param name="parameters">請求引數</param>
/// <param name="method">請求方法</param>
/// <returns>響應內容</returns>
public string sendPost(string url, IDictionary<string, string> parameters, string method)
{
    if (method.ToLower() == "post")
    {
        HttpWebRequest req = null;
        HttpWebResponse rsp = null;
        System.IO.Stream reqStream = null;
        try
        {
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = method;
            req.KeepAlive = false;
            req.ProtocolVersion = HttpVersion.Version10;
            req.Timeout = 5000;
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
            reqStream = req.GetRequestStream();
            reqStream.Write(postData, 0, postData.Length);
            rsp = (HttpWebResponse)req.GetResponse();
            Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
            return GetResponseAsString(rsp, encoding);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            if (reqStream != null) reqStream.Close();
            if (rsp != null) rsp.Close();
        }
    }
    else
    {
        //建立請求
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));

        //GET請求
        request.Method = "GET";
        request.ReadWriteTimeout = 5000;
        request.ContentType = "text/html;charset=UTF-8";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

        //返回內容
        string retString = myStreamReader.ReadToEnd();
        return retString;
    }
}
/// <summary>
/// 組裝普通文字請求引數。
/// </summary>
/// <param name="parameters">Key-Value形式請求引數字典</param>
/// <returns>URL編碼後的請求資料</returns>
public string BuildQuery(IDictionary<string, string> parameters, string encode)
{
    StringBuilder postData = new StringBuilder();
    bool hasParam = false;
    IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
    while (dem.MoveNext())
    {
        string name = dem.Current.Key;
        string value = dem.Current.Value;
        // 忽略引數名或引數值為空的引數
        if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
        {
            if (hasParam)
            {
                postData.Append("&");
            }
            postData.Append(name);
            postData.Append("=");
            if (encode == "gb2312")
            {
                postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
            }
            else if (encode == "utf8")
            {
                postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
            }
            else
            {
                postData.Append(value);
            }
            hasParam = true;
        }
    }
    return postData.ToString();
}

5、提交成功咯~

請喊我大龍哥最後編輯於:2年前

內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章