Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-HttpWebRequest帖子(登录Facebook)_C#_Facebook_Post_Login_Httpwebrequest - Fatal编程技术网

C#-HttpWebRequest帖子(登录Facebook)

C#-HttpWebRequest帖子(登录Facebook),c#,facebook,post,login,httpwebrequest,C#,Facebook,Post,Login,Httpwebrequest,我正试图在我的程序中登录到Facebook并解析其中的一些信息(如姓名、个人资料图片等) 每次执行下面的代码,我都会被重定向回Facebook的主页 string email = "email"; string pw = "pw"; string PostData = String.Format("email={0}&pass={1}", email, pw); CookieContainer cookieContainer = new CookieContainer(); Http

我正试图在我的程序中登录到Facebook并解析其中的一些信息(如姓名、个人资料图片等)

每次执行下面的代码,我都会被重定向回Facebook的主页

string email = "email";
string pw = "pw";
string PostData = String.Format("email={0}&pass={1}", email, pw);

CookieContainer cookieContainer = new CookieContainer();

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentLength = PostData.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(PostData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);

HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();

Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
webBrowser1.DocumentText = reader.ReadToEnd();

foreach (Cookie cookies in webResp.Cookies)
{
    MessageBox.Show(cookies.Name + "   " + cookies.Value);
}
我做错了什么?任何帮助都将不胜感激,非常感谢!:)

编辑: 我在发帖子后不久就知道了怎么做

每次你访问Facebook查看是否启用了cookie时,Facebook都会发送一个cookie,我所做的是向Facebook的登录页面发送一个请求以获取cookie,然后发送另一个带有POST数据的请求。它就是这样工作的,我成功地登录了


无论如何,谢谢!:)

有时使用web浏览器更容易,您可以隐藏在后台运行该浏览器。稍后,您可以获取内部html,或者下载图像。不管你需要做什么。下面是一个使用windows窗体的示例

创建一个新的win forms应用程序,添加一个按钮并将代码粘贴到其中。不要添加任何其他内容,它应该可以工作

private void按钮1\u单击(对象发送者,事件参数e)
{
string email=“您的电子邮件”;
string password=“您的密码”;
//创建新浏览器
WebBrowser w=新的WebBrowser();
w、 Dock=DockStyle.Fill;
this.Controls.Add(w);//如果要查看正在发生的情况,可以将该控件添加到windows窗体中
//后者您可能不会选择添加浏览器,甚至可以将其设置为不可见。。。
//导航到facebook
w、 导航(@)http://www.facebook.com/");
//稍等
对于(int i=0;i<100;i++)
{
系统线程线程睡眠(10);
System.Windows.Forms.Application.DoEvents();
}
HtmleElement temp=null;
//当我们通过id找到一个名为email的元素时
while(temp==null)
{
temp=w.Document.GetElementById(“电子邮件”);
系统线程线程睡眠(10);
System.Windows.Forms.Application.DoEvents();
}
//一旦我们找到它,就把它的价值
临时设置属性(“值”,电子邮件);
温度=零;
//存在id为pass的wiat till元素
while(temp==null)
{
temp=w.Document.GetElementById(“通过”);
系统线程线程睡眠(10);
System.Windows.Forms.Application.DoEvents();
}
//一旦存在,将其值设置为passowrd
临时设置属性(“值”,密码);
//如果您已经找到最后的字段,按钮也应该在那里。。。
var inputs=w.Document.GetElementsByTagName(“输入”);
int计数器=0;
bool enableClick=false;
//迭代文档中的所有输入
foreach(输入中的HtmlElement btn)
{
尝试
{
var att=btn.GetAttribute(“tabindex”);
var name=btn.GetAttribute(“id”);
if(enableClick)//提交的按钮总是有一个不同的id。它应该在密码文本框之后
{
btn.InvokeMember(“点击”);
计数器++;
}
if(name.ToUpper().Contains(“PASS”)| | att==“4”)
{
enableClick=true;//按钮应位于密码输入旁边
}
//最多尝试5次
如果(计数器>5)
打破
}
抓住
{
}
}
}

我很高兴你找到了答案,我也可以像你说的那样,使用
HttpWebRequest
登录facebook。。以下是一个可接受的解决方法:

第一个请求:获取cookies

CookieCollection cookies=新的CookieCollection();
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(“https://www.facebook.com); 
request.CookieContainer=新的CookieContainer();
request.CookieContainer.Add(cookies);
//从服务器获取响应并保存第一个请求中的cookie。。
HttpWebResponse=(HttpWebResponse)request.GetResponse();
cookies=响应。cookies;
第二个请求:发布表单数据并从第一个请求中恢复cookie

string getUrl=”https://www.facebook.com/login.php?login_attempt=1";
stringpostdata=string.Format(“email={0}&pass={1}”、“value1”、“value2”);
HttpWebRequest getRequest=(HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer=新的CookieContainer();
getRequest.CookieContainer.Add(cookies);//第一次请求恢复cookies
getRequest.Method=WebRequestMethods.Http.Post;
getRequest.UserAgent=“Mozilla/5.0(Windows NT 6.1)AppleWebKit/535.2(KHTML,类似Gecko)Chrome/15.0.874.121 Safari/535.2”;
getRequest.AllowWriteStreamBuffering=true;
getRequest.ProtocolVersion=HttpVersion.Version11;
getRequest.AllowAutoRedirect=true;
getRequest.ContentType=“应用程序/x-www-form-urlencoded”;
byte[]byteArray=Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength=byteArray.Length;
Stream newStream=getRequest.GetRequestStream();//打开连接
newStream.Write(byteArray,0,byteArray.Length);//发送数据。
newStream.Close();
HttpWebResponse getResponse=(HttpWebResponse)getRequest.getResponse();
使用(StreamReader sr=newstreamreader(getResponse.GetResponseStream()))
{
字符串sourceCode=sr.ReadToEnd();
} 
这是使用
HttpWebRequest
的众多方法之一,如果您更喜欢使用WebBrowser,也可以使用以下方法:

webBrowser1.导航(“https://www.facebook.com/login.php?login_attempt=1“,”,byteArray,“内容类型:application/x-www-form-urlencoded”);

我刚刚意识到它不适用于
InternetSetCookie
,可能是因为从Facebook返回的Cookie具有“HttpOnly”属性(true),并且客户端脚本无法访问它。

Facebook有一个API,这是有原因的,对于问题提出的内容来说,抓取HTML很难是一个体面的建议。我说过解决方案不是最好的
webbrowser1.navigate("www.facebook.com");
webbrowser1.GetElementByiId("email").value="your mail ID";
webbrowser1.GetElementByiId("pass").value="password";
HtmlElement form = webBrowser1.Document.GetElementById("FormID");
if (form != null)
form.InvokeMember("submit");