C# 通过HttpWebRequest post 方法获取cookie

C#通过HttpWebRequestpost方法获取cookie,下面是我在网上搜了一段代码,要发给服务器的postDataStr怎么得到???我随便填的一些发现程序会报... C# 通过HttpWebRequest post 方法获取cookie ,下面是我在网上搜了一段代码,要发给服务器的postDataStr怎么得到???我随便填的一些 发现程序会报错 求解 、、、、

public string SendDataByPost(string Url, string postDataStr, ref CookieContainer cookie)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

return retString;
}
展开
 我来答
kingboyf4
推荐于2016-11-14 · TA获得超过283个赞
知道小有建树答主
回答量:201
采纳率:100%
帮助的人:240万
展开全部

给你一个新HttpRequest类,我以前用来做社区游戏辅助用的。

namespace RainyManor.Common
{
    public class myHttpRequest
    {
        private CookieContainer _cc = new CookieContainer();
        public myHttpRequest(CookieContainer cc)
        {
            if (cc != null)
                _cc = cc;
        }
        public CookieContainer cookies
        {
            get { return _cc; }
        }
        private HttpWebRequest GetRequest(string uri, string method)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = method;
            request.Accept = Resources.Accept;
            request.UserAgent = Resources.UserAgent;
            request.ContentType = Resources.ContentType;
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
            request.Timeout = 20000;
            return request;
        }
        public string GetPageSource(string url)
        {
            HttpWebRequest request = GetRequest(url, "get");
            request.CookieContainer = _cc;
            return GetStringFromRequest(request, myEncoding.GB2312);
        }
        private string GetStringFromRequest(HttpWebRequest request, Encoding encode)
        {
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, encode))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch
            {
                return string.Empty;
            }
        }
        public string GetSourceByPost(string url, string postData)
        {
            try
            {
                byte[] bytes = Encoding.ASCII.GetBytes(postData);
                HttpWebRequest request = GetRequest(url, "post");
                request.CookieContainer = _cc;
                Stream sendStream = request.GetRequestStream();
                sendStream.Write(bytes, 0, bytes.Length);
                sendStream.Close();
                return GetStringFromRequest(request, myEncoding.GB2312);
            }
            catch
            {
                return "";
            }
        }
        public string GetSourceByPostAction(string url, string postData)
        {
            try
            {
                byte[] bytes = Encoding.ASCII.GetBytes(postData);
                HttpWebRequest request = GetRequest(url, "post");
                request.ContentType = "text/xml";
                request.CookieContainer = _cc;
               
                Stream sendStream = request.GetRequestStream();
                sendStream.Write(bytes, 0, bytes.Length);
                sendStream.Close();

                return GetStringFromRequest(request, myEncoding.UTF8);
            }
            catch
            {
                return "";
            }
        }
    }
}

1. Resources.Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*

2. Resources.UserAgent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; MS-RTC LM 8)

3. Resources.ContentType:application/x-www-form-urlencoded

G0398
推荐于2018-04-11 · TA获得超过764个赞
知道小有建树答主
回答量:1335
采纳率:0%
帮助的人:881万
展开全部
postDataStr
是表单中提交的数据 随便填的话服务端不可能解析的 所以就报错了
你可以在浏览器中点F12 网络 查看post提交的数据 把这些数据复制即可
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式