Net Core教程

C#模拟登录网页---HttpWebRequest

本文主要是介绍C#模拟登录网页---HttpWebRequest,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

模拟登录开源众包平台

1、打开开发者工具,找到登录api
在这里插入图片描述
2、在Payload中查看到Post的数据格式
在这里插入图片描述

3、模拟登陆

HttpWebRequest request = HttpWebRequest.CreateHttp("https://zb.oschina.net/login/doLogin");
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            using (Stream request_stream = request.GetRequestStream())
            {                    
                byte[] body = Encoding.UTF8.GetBytes("{\"username\":\"*****\",\"password\":\"****\"}");
                request_stream.Write(body, 0, body.Length);
            }
            HttpWebResponse httpresponse = (HttpWebResponse)request.GetResponse();
            string response= "";
            using (StreamReader reader = new StreamReader(httpresponse .GetResponseStream(), Encoding.UTF8))
            {
                response = reader.ReadToEnd();
                return response;
            }

4、获取网页源代码

 HttpWebRequest request = HttpWebRequest.CreateHttp("https://zb.oschina.net/projects/list.html");
            HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse;
            using(StreamReader sr=new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
            {
                string responseText= sr.ReadToEnd();
                return responseText;
            }
这篇关于C#模拟登录网页---HttpWebRequest的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!