Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm building an winform application that posts articles programmatically on my website using htttpwebrequest.
It shows a message "The remote server returned an error: (403) "
When I try to sign in by manually,it works but sigining in by program doesn't work.
The source code works for other website but it doesn't work for this website
What am I missing?
Can anybody help me?


Thank you

Here is the source code

C#
private void btn_insert_Click(object sender, EventArgs e)
{
    MakeAPost("title3", "contents3");
}

private void MakeAPost(string title, string contents)
{
    //sendData is sign in info
    string sendData = "csrf_test_name=0bc950796216122f470f7a0714304097&url=&mem_userid=Mylogin_id&mem_password=Mypassword";
    string result = RequestWebPage("http://mydomain.com/login", sendData, cookie, title, contents);

    textBox1.Text = result;

}

public static string RequestWebPage(string url, string sendData, CookieContainer cook, string title, string contents)
{
    try
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);


        req.Method = "POST";
        req.Accept = "text/html, application/xhtml+xml, */*";
        req.Referer = "http://mydomain.com/login";
        req.Headers.Add("Accept-Language", "ko-KR");
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
        req.ContentType = "application/x-www-form-urlencoded";
        req.Headers.Add("Accept-Encoding", "gzip, deflate");
        req.Host = "mydomain.com";

        req.ContentLength = sendData.Length;
        req.KeepAlive = true;
        req.Headers.Add("Pragma", "no-cache");
        req.ServicePoint.Expect100Continue = false;

        req.CookieContainer = cook;

        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        writer.Write(sendData);
        writer.Close();


        HttpWebResponse result = (HttpWebResponse)req.GetResponse();
        Stream respStream = result.GetResponseStream();
        string respStr = new StreamReader(respStream).ReadToEnd();
        cookie.Add(result.Cookies);

        if (result.StatusCode == HttpStatusCode.OK)
        {

           return "Sucess";

        }

        else
        {
            Console.WriteLine("Error");
            return "ERR";
        }

    }

    catch (WebException webex)
    {

        MessageBox.Show(webex.Message);
    }
    finally
    {

    }
    return null;
}


What I have tried:

I have been googling so many times but I can't fix my problem
Posted
Updated 31-Mar-16 5:34am
v5
Comments
Rob Philpott 31-Mar-16 9:07am    
Are you on an intranet, or something which involves going through a proxy?

1 solution

The site uses Cross-Site Request Forgery Prevention which is a fancy way of saying that it has been coded in such a way to stop you doing what you are doing, so they clearly don't want people automating their site.
 
Share this answer
 
Comments
hapiten 31-Mar-16 10:55am    
Thank you for your comment.
Then I can't insert any data by program?
Is there any way how to insert it?

Thank you
F-ES Sitecore 31-Mar-16 11:03am    
Ask them how you can submit data to their site programmatically.
hapiten 31-Mar-16 11:29am    
The website administrator has changed it that I can sign in automaticaly.
Thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900