public static string PostUrl(string[] urls) { try { string formUrl = "http://data.zz.baidu.com/urls?site=www.yoursite.com&token=yourcode"; string formData = ""; foreach (string url in urls) { formData += url + "\n"; } byte[] postData = System.Text.Encoding.UTF8.GetBytes(formData); // 設置提交的相關參數 System.Net.HttpWebRequest request = System.Net.WebRequest.Create(formUrl) as System.Net.HttpWebRequest; System.Text.Encoding myEncoding = System.Text.Encoding.UTF8; request.Method = "POST"; request.KeepAlive = false; request.AllowAutoRedirect = true; request.ContentType = "text/plain"; request.UserAgent = "curl/7.12.1"; request.ContentLength = postData.Length; // 提交請求數據 System.IO.Stream outputStream = request.GetRequestStream(); outputStream.Write(postData, 0, postData.Length); outputStream.Close(); System.Net.HttpWebResponse response; System.IO.Stream responseStream; System.IO.StreamReader reader; string srcString; response = request.GetResponse() as System.Net.HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8")); srcString = reader.ReadToEnd(); string result = srcString; //返回值賦值 reader.Close(); return result; } catch(Exception ex) { return ex.Message; } }
調用html
string info = PostUrl(new string[] { "http://www.xxx.com/1.html", "http://www.xxx.com/2.html" });
返回的結果是{"remain":498,"success":2} 表示已經推送成功,還剩498條能夠推送,本次已經推送成功2條。服務器
另外附上可能出現的異常狀況的返回碼信息,供調試用:
200 無使用方式錯誤,須要進一步觀察返回的內容是否正確
400 必選參數未提供
405 不支持的請求方式,咱們只支持POST方式提交數據
411 HTTP頭中缺乏Content-Length字段
413 推送的數據過大,超過了10MB的限制
422 HTTP頭中Content-Length聲明的長度和實際發送的數據長度不一致
500 站長平臺服務器內部錯誤
curl