gpt4 book ai didi

c# - 如何使用asp.net登录其他网站?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:19:25 27 4
gpt4 key购买 nike

我正在尝试制作一个可以为用户登录其他网站并获取某些信息的网站。例如:一个基于网络的游戏论坛,可以自动提取您的游戏统计信息。我的网站必须导航到游戏 url,填写用户名和密码,登录,然后在登录后阅读 html(这是简单的部分,为此我只需使用 HTML 敏捷包或类似的东西) .这个登录过程可以用 asp.net 实现吗?

最佳答案

是的。

不要把它想象成转到页面并填写内容。您将需要创建一个 web request。包含正确的 HTTP header 和数据的服务器,它们会查看他们的服务器是否填写了所有内容。您需要查看您尝试使用的网站以了解具体情况,但我假设他们'单击登录时重新发送一组 POST 数据。示例,从 MSDN 中提取并修改为更接近您的需要(前几行):

WebRequest request = WebRequest.Create ("http:/www.site.com/loginPostback");
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "username=blah;password=blah";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();

然后您可以检查服务器在登录后发回的 HTML 响应流,以获取您需要的信息。

话虽如此,您可能想联系他们,看看他们是否有 API。如果他们更改登录表单的工作方式,您就会崩溃。或者,如果该站点支持 OAuth 之类的东西,那就走那条路。

关于c# - 如何使用asp.net登录其他网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5315136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com