Httpwebrequest远程服务器在C#中返回错误:(403) [英] Httpwebrequest the remote server returned an error: (403) in C#

查看:327
本文介绍了Httpwebrequest远程服务器在C#中返回错误:(403)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个winform应用程序,使用htttpwebrequest在我的网站上以编程方式发布文章。

它显示一条消息远程服务器返回错误:(403)

当我尝试手动登录时,它可以正常工作,但程序中的sigining不起作用。

源代码适用于其他网站,但它不适用于本网站

我错过了什么?

有人能帮帮我吗?





谢谢



这是源代码



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

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

推荐答案

该网站使用跨站点请求伪造预防这是一个花哨的说法,它已被编码,以阻止你做你正在做的事情,所以他们显然不希望人们自动化他们的网站。
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.


这篇关于Httpwebrequest远程服务器在C#中返回错误:(403)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆