2 zui2jun zui2jun 于 2016.03.31 16:52 提问

c#模拟登陆 返回500错误

我想用c#模拟登陆一个网站,然后再对网站进行操作,post参数也很简单,但是为什么返回500错误呢?
图片说明
图片说明
图片说明
图片说明
图片说明

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace emulateLoginBaidu
{
    class HTMLHelper
    {
        public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                CookieContainer cc = new CookieContainer();
                request = (HttpWebRequest)WebRequest.Create(loginUrl);
                request.Method = header.method;
                request.ContentType = header.contentType;
                byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabyte.Length;
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;

                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabyte, 0, postdatabyte.Length);
                stream.Close();

                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

                CookieCollection cook = response.Cookies;
                //Cookie字符串格式
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

                return cc;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 获取html
        /// </summary>
        /// <param name="getUrl"></param>
        /// <param name="cookieContainer"></param>
        /// <param name="header"></param>
        /// <returns></returns>
        public static string GetHtml(string getUrl, CookieContainer cookieContainer, HttpHeader header)
        {
            Thread.Sleep(1000);
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = header.contentType;
                httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
                httpWebRequest.Referer = getUrl;
                httpWebRequest.Accept = header.accept;
                httpWebRequest.UserAgent = header.userAgent;
                httpWebRequest.Method = "GET";
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                httpWebRequest.Abort();
                httpWebResponse.Close();
                return html;
            }
            catch (Exception e)
            {
                if (httpWebRequest != null) httpWebRequest.Abort();
                if (httpWebResponse != null) httpWebResponse.Close();
                return string.Empty;
            }
        }
    }
    public class HttpHeader
    {
        public string contentType { get; set; }

        public string accept { get; set; }

        public string userAgent { get; set; }

        public string method { get; set; }

        public int maxTry { get; set; }
    }

}

测试代码:

 try
            {
                HttpHeader header = new HttpHeader();
                header.accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                header.contentType = "application/x-www-form-urlencoded";
                header.method = "POST";
                header.userAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
                header.maxTry = 300;

                CookieContainer cookieContainer = new CookieContainer();
                cookieContainer = HTMLHelper.GetCooKie("https://www.wish.com/api/email-login", "email=oxvbgzo66627404%40163.com&password=ai49425&_buckets=&_experiments=", header);
                string html = HTMLHelper.GetHtml("https://www.wish.com", cookieContainer, header);
                MessageBox.Show(html);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

2个回答

caozhy
caozhy   Ds   Rxr 2016.03.31 18:23
已采纳

所有的字段都要和浏览器访问的一样,而不是仅仅是参数。

zui2jun
zui2jun 厉害 大神果然不一样 解决了
一年多之前 回复
zui2jun
zui2jun 厉害 大神果然不一样 解决了
一年多之前 回复
zui2jun
zui2jun 请求标头要写哪些数据?
一年多之前 回复
CSDNXIAON
CSDNXIAON   2016.03.31 17:03

C#模拟登陆核心代码
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!