简体   繁体   中英

C#: HttpWebRequest POST data not working

I am developing a C# wpf application that has a functionality of logging into my website and download the file. This said website has an Authorize attribute on its action. I need 2 cookies for me to able to download the file, first cookie is for me to log in, second cookie(which is provided after successful log in) is for me to download the file. So i came up with the flow of keeping my cookies after my httpwebrequest/httpwebresponse. I am looking at my posting flow as maybe it is the problem. Here is my code.

void externalloginanddownload()
{
    string pageSource = string.Empty;
    CookieContainer cookies = new CookieContainer();

    HttpWebRequest getrequest = (HttpWebRequest)WebRequest.Create("login uri");
    getrequest.CookieContainer = cookies;
    getrequest.Method = "GET";
    getrequest.AllowAutoRedirect = false;

    HttpWebResponse getresponse = (HttpWebResponse)getrequest.GetResponse();

    using (StreamReader sr = new StreamReader(getresponse.GetResponseStream()))
    {
        pageSource = sr.ReadToEnd();
    }

    var values = new NameValueCollection
    {
                {"Username", "username"},
                {"Password", "password"},
        { "Remember me?","False"},
            };

    var parameters = new StringBuilder();

    foreach (string key in values.Keys)
    {
        parameters.AppendFormat("{0}={1}&",
            HttpUtility.UrlEncode(key),
            HttpUtility.UrlEncode(values[key]));
    }

    parameters.Length -= 1;

    HttpWebRequest postrequest = (HttpWebRequest)WebRequest.Create("login uri");
    postrequest.CookieContainer = cookies;
    postrequest.Method = "POST";

    using (var writer = new StreamWriter(postrequest.GetRequestStream()))
    {
        writer.Write(parameters.ToString());
    }

    using (WebResponse response = postrequest.GetResponse()) // the error 500 occurs here
    {
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            string html = streamReader.ReadToEnd();

        }
    }
}

When you get the WebResponse, the cookies returned will be in the response, not in the request (oddly enough, even though you need to CookieContainer on the request).

You will need to add the cookies from the response object to your CookieContainer, so it gets sent on the next request.

One simple way:

for(var cookie in getresponse.Cookies) 
    cookies.Add(cookie)

Since the cookies in response is already a cookies container, you can do this (might help to check for null in case all cookies were already there)

if (response.Cookies != null) cookies.Add(response.Cookies)

You may also have trouble with your POST as you need to set ContentType and length:

myWebRequest.ContentLength = parameters.Length;
myWebRequest.AllowWriteStreamBuffering = true;

If you have any multibyte characters to think about, you may have to address that as well by setting the encoding to UTF-8 on the request and the stringbuilder, and converting string to bytes and using that length.

Another tip: some web server code chokes if there is no user agent. Try:

myWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

And just in case you have any multibyte characters, it is better to do this:

var databytes = System.Text.Encoding.UTF8.GetBytes(parameters.ToString());
myWebRequest.ContentLength = databytes.Length;
myWebRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
using (var stream = myWebRequest.GetRequestStream())
{
    stream.Write(databytes, 0, databytes.Length);
}

In C# Application (Server side Web API) Enable the C++ Exception and Common Language Run time Exceptions using (Ctrl+Alt+E) what is the Server side Exception it's throw.

First you check data is binding Properly. After you can see what it is Exact Exception. the Internal Server Error Mostly throw the data is not correct format and not properly managed Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM