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)
{
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