C# webrequest和response上的cookies处理

C# webrequest和response上的cookies处理,c#,C#,我创建了一个具有函数mainpost的应用程序。创建它是为了在https站点上发布数据。这里我想在这个函数中处理cookies。我怎样才能完成这项任务 public string Mainpost(string website, string content) { // this is what we are sending string post_data = content; // this is where we will send it string u

我创建了一个具有函数
mainpost
的应用程序。创建它是为了在https站点上发布数据。这里我想在这个函数中处理cookies。我怎样才能完成这项任务

public string Mainpost(string website, string content)
{
    // this is what we are sending
    string post_data = content;

    // this is where we will send it
    string uri = website;

    // create a request
    HttpWebRequest request = (HttpWebRequest)
    WebRequest.Create(uri); request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;
    request.Method = "POST";

    // turn our request string into a byte stream
    byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

    // this is important - make sure you specify type this way
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = postBytes.Length;
    Stream requestStream = request.GetRequestStream();

    // now send it
    requestStream.Write(postBytes, 0, postBytes.Length);
    requestStream.Close();

    // grab te response and print it out to the console along with the status
    // code
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string str = (new StreamReader(response.GetResponseStream()).ReadToEnd());
    Console.WriteLine(response.StatusCode);

    return str;
}
您需要创建一个对象,并将每个
HttpWebRequest
CookieContainer
属性设置为
CookieContainer
实例。

您需要设置请求之间的间隔

如果您维护同一个cookie容器,将来的请求将重新发送cookie