C# 使用HttpWebRequest Post提交資料,攜帶Cookie和相關參數樣本

來源:互聯網
上載者:User

標籤:static   read   ica   sms   網頁   pass   gecko   form   控制   

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Configuration;using System.Data.SqlClient;using System.Data;using System.Diagnostics;using System.Net;using System.IO;namespace Sample2{    class Program    {        static void Main(string[] args)        {            try            {                //data                string cookieStr = "51fd9f14fa7561b5";                string postData = string.Format("userid={0}&password={1}", "guest", "123456");                byte[] data = Encoding.UTF8.GetBytes(postData);                // Prepare web request...                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.xxx.com");                request.Method = "POST";                //request.Referer = "https://www.xxx.com";                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";                //request.Host = "www.xxx.com";                request.Headers.Add("Cookie", cookieStr);                request.ContentLength = data.Length;                Stream newStream = request.GetRequestStream();                // Send the data.                newStream.Write(data, 0, data.Length);                newStream.Close();                // Get response                HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);                string content = reader.ReadToEnd();                Console.WriteLine(content);                Console.ReadLine();            }            catch (Exception)            {                throw;            }        }    }}
/// <summary>/// 調用簡訊介面傳送簡訊,需配合模板/// </summary>/// <param name="userName">介面使用者名稱</param>/// <param name="userPwd">介面密碼</param>/// <param name="mobile">發送手機號碼</param>/// <param name="content">發送內容</param>/// <returns> 傳回值大於0,發送成功,系統產生的任務id或自訂的任務id</returns>public static string PostSms(string userName, string userPwd, string mobile, string content){    string srcString = "-999";    try    {        string postString = string.Format("username={0}&password={1}&mobile={2}&content={3}", userName, MD5(userName + MD5(userPwd)), mobile, content);        byte[] postData = Encoding.UTF8.GetBytes(postString);//編碼,尤其是漢字,事先要看下抓取網頁的編碼方式          string url = "http://xxx/smsSend.do";//地址          WebClient webClient = new WebClient();        webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//採取POST方式必須加的header,如果改為GET方式的話就去掉這句話即可          byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字元流          srcString = Encoding.UTF8.GetString(responseData);//解碼    }    catch { }    return srcString;}
    class Program    {        static void Main(string[] args)        {            var str = DownloadStringAsync(new Uri("http://www.baidu.com"));            Console.WriteLine(str.Result);            Console.ReadLine();        }        static async Task<string> DownloadStringAsync(Uri uri)        {            //WebClient 不支援逾時設定            //而HttpWebRequst則允許你佈建要求頭或者對內容需要更多的控制            var webClient = new WebClient();            webClient.Encoding = System.Text.Encoding.UTF8;            var result = await webClient.DownloadStringTaskAsync(uri);            return result;        }    }

 

C# 使用HttpWebRequest Post提交資料,攜帶Cookie和相關參數樣本

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.