我想用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);
}
所有的字段都要和浏覽器訪問的一樣,而不是僅僅是參數。