继承 httpwebrequest和HttpWebResponse类时遇到错误 CS1729
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承 httpwebrequest和HttpWebResponse类时遇到错误 CS1729相关的知识,希望对你有一定的参考价值。
出问题的代码是这样的:
public class MyWebRequest:System.Net.HttpWebRequest
【错误在这一行】 public MyWebRequest(Uri uri, bool bKeepAlive, string proxyName, int proxyPort)
Headers = new WebHeaderCollection();
RequestUri = uri;
Headers["Host"] = uri.Host;
错误是 错误 CS1729: “System.Net.HttpWebRequest”不包含采用“0”参数的构造函数
c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll: (相关文件)
和
public class MyWebResponse:System.Net.HttpWebResponse
【错误在这行】 public MyWebResponse()
【没错,这里什么也没有】
错误 CS1729: “System.Net.HttpWebResponse”不包含采用“0”参数的构造函数
请问这是什么原因,如何解决?
谢谢
PS:这个程序里面的mywebrequest类是已经写好的,很多地方要调用,改起来很麻烦。但是它里面貌似没有cookiecontainer,现在应用上恐怕要用到,所以我想通过继承让它能够使用cookiecontainer。
protected HttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext);
internal HttpWebResponse(Uri responseUri, KnownHttpVerb verb, CoreResponseData coreData, string mediaType, bool usesProxySemantics, DecompressionMethods decompressionMethod);
你定义的构造函数,在System.Net.HttpWebResponse中未定义,所以报些编译错误
参考资料:http://msdn.microsoft.com/zh-cn/library/bb384029.aspx
参考技术B 继承的时候会隐式的调用父类无参的构造函数。改成:
public MyWebRequest(Uri uri, bool bKeepAlive, string proxyName, int proxyPort) :base(参数)
Headers = new WebHeaderCollection();
RequestUri = uri;
Headers["Host"] = uri.Host;
。。。。。
本回答被提问者采纳
获取登录cookieColletion在cef里面打开网页
public static BaseResult getHTML(string url, Encoding encoding, string cookieString) { CookieContainer loginCookies = new CookieContainer(); BaseResult result = new BaseResult(); HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url); //建立HttpWebRequest對象 hwr.Method = "GET"; hwr.Timeout = 60000; //定義服務器超時時間 //hwr.AllowAutoRedirect = false; hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36"; hwr.Headers.Add("content", "text/html; charset=GBK"); if (cookieString != null) { hwr.Headers.Add("Cookie", cookieString); } else { hwr.CookieContainer = loginCookies; } HttpWebResponse hwResponse = null; try { lock (obj) { hwResponse = (HttpWebResponse)hwr.GetResponse(); //取得回應 //判断HTTP响应状态 if (hwResponse.StatusCode != HttpStatusCode.OK) { result.Code = 0; result.Message = "访问失败!状态码:" + hwResponse.StatusCode; } else { result.Code = 1; result.Message = "成功"; Stream s = hwResponse.GetResponseStream(); //得到回應的流對象 StreamReader sr = new StreamReader(s, encoding); //以UTF-8編碼讀取流 StringBuilder content = new StringBuilder(); // while (sr.Peek() != -1) //每次讀取一行,直到 { //下一個字節沒有內容 content.Append(sr.ReadLine()); //返回為止 } result.Content = content.ToString(); result.Cookie = hwResponse.Headers.Get("Set-Cookie"); result.cookieColletion = hwResponse.Cookies; } } } catch (Exception ex) { result.Code = -1; result.Message = "请求失败,请检查地址"; result.Content = ex.ToString(); } finally { if (hwResponse != null) { hwResponse.Close(); } hwr = null; } return result; }
设置
private void SetCefCookies(string url, CookieCollection cookies) { CefSharp.Cef.GetGlobalCookieManager().SetStoragePath(Environment.CurrentDirectory, true); foreach (System.Net.Cookie c in cookies) { var cookie = new CefSharp.Cookie { Creation = DateTime.Now, Domain = c.Domain, Name = c.Name, Value = c.Value, Expires = c.Expires }; CefSharp.Cef.GetGlobalCookieManager().SetCookie(url, cookie); } }
以上是关于继承 httpwebrequest和HttpWebResponse类时遇到错误 CS1729的主要内容,如果未能解决你的问题,请参考以下文章