开发者

c# Post data cookies not working

Alright, so I've got a function I wrote that should allow me to post data with cookies. The problem is is I'm testing it out on an Amazon login page, and it keeps responding saying I need cookies enabled. Here's the code

public string DoPost(String url, PostData data, CookieContainer cookies)
{
    HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(url);
    objWebRequest.CookieContainer = cookies;
    objWebRequest.AllowAutoRedirect = true;
    objWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    if(data != null)
    {
        String post = data.ToString();
        objWebRequest.Method = "POST";
        objWebRequest.ContentLength = post.Length;
        objWebRequest.ContentType = "application/x-www-form-urlencoded";
        // Post to the login form.
        using(StreamWriter swRequestWriter = new StreamWriter(objWebRequest.GetRequestStream()))
        {
            swRequestWriter.Write(post);
        }
    }

    // Get the response.
    HttpWebResponse objWebResponse =
         (HttpWebResponse)objWebRequest.GetResponse();

    // Read the response
    using(StreamReader srResponseReader = new StreamReader(objWebResponse.GetResponseStream()))
    {
        string strResponseData = srResponseReader.ReadToEnd();
        return strResponseData;
    }
}

And I call it like this

String action = "https://www.amazon.com/gp/flex/sign-in/select.html";
String s = DoPost(action, null, Cookies);

Cookies is created in my class constructer like this

CookieContainer Cookies;

public Constructz0r()
{
    Cookies = new CookieContainer();
}

The thing is, I'm not even posting any post data, I'm just going to the page, and it's saying my cookies aren't enabled, though I feel I've done it write in DoPost.

I've even tried using this implementation of WebClient

    public class CookieWebClient : WebClient
    {
        private CookieContainer _cookieContainer = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = _cookieContainer;
            }
            return request;
        }
}

And calling it like this

        using(CookieWebClient ck = new CookieWebClient())
        {
            String s =  ck.Downl开发者_运维技巧oadString(action);
        }

And it still tells me the cookies aren't enabled.


Amazon have an API to access their services (SOAP). So instead of trying to do some scraping I would strongly recommend you using their API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜