2017-01-02 117 views
1

说我有一个网页,输入提示,然后搜索用户的用户名和密码的数据库,然后解密密码,并检查输入的密码是一样的用户的密码在数据库中。如果我想将登录切换到C#winforms应用程序,该怎么办?我如何才能与用户名和输入的密码是HTTP请求,然后有网站接受用户名/密码字符串和搜索数据库对于那些办法我前面说的一样,然后发送一个真布尔:用户输入正确的信息或false:用户输入了错误的信息。我怎么能这样做?如何使一个HTTP请求,并获得在C#中的响应

+0

为什么'php'标签在这里?你应该打电话给哪个API? –

+0

所以你想从Windows窗体调用该网站? – CodingYoshi

+1

这里尝试http://stackoverflow.com/questions/4088625/net-simplest-way-to-send-post-with-data-and-read-response看 – Beginner

回答

2

要做到这一点,首先需要知道服务器是如何接受,因为你必须输入用户名和密码,这是最有可能是由POST方法的请求,那么你将获得的输入可以是字符串使用 一些浏览器扩展名获得,如Live Http Headers。 它应该是这个样子

http://yourwebsite/extension 

POST /extension HTTP/1.1 
Host: yourwebsite 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Content-Length: 85 
Content-Type: application/x-www-form-urlencoded 
Connection: keep-alive 
HereShouldBeThePoststring 

然后,你将创建一个HttpWebRequest的使用本网站的URL

  using System.Net; 
 
      string postUrl = "YourWebsiteUrlWithYourExtension"; 
 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);

那么你会发布数据

string postString = "ThePostStringYouObtainedUsingLiveHttpHeaders"; 
 
      request.Method = "POST"; 
 
      byte[] Content = Encoding.ASCII.GetBytes(postString); 
 
      request.ContentLength = Content.Length; 
 
      using (Stream stream = request.GetRequestStream()) 
 
      { 
 
       stream.W

,那么你会得到响应串

string responsestring = null; 
 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
 
      using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
 
      { 
 
       responsestring = reader.ReadToEnd(); 
 
      }

那么你会分析你的字符串来获取数据您的需要。 解析的好库是Html Agility Pack

相关问题