Tag: httpwebrequest webclient

带有自定义标头的Windows Phone 8 Http请求

我想从Windows Phone 8向WCF服务器发送HTTP PUT请求,为了识别,我必须发送自定义标头。 (假设“mycustomheader”=“abc”) 到目前为止我使用的是WebClient ,但是Webclient.Headers似乎没有Add方法,因此除了HttpRequestHeader枚举中的标题之外,不可能发送标题。 有没有办法用WebClient做到这一点? 我看到可以使用HttpWebRequest类设置自定义标头,但我根本无法让它做任何事情。 我的测试代码(基本上是从http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx复制的样本): public void dosth() { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(“http://mycomputer/message”); wr.Method = “PUT”; wr.ContentType = “application/x-www-form-urlencoded”; wr.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), wr); allDone.WaitOne(); } private static void GetRequestStreamCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; Stream postStream = request.EndGetRequestStream(asynchronousResult); string postData = “{‘Command’: { ‘RequestType’ : ‘Status’, ‘Test’ : ‘1’ }}”; […]

使用WebClient或HttpWebRequest登录

我有严重的登录问题。 我不能使用WebBrowser类登录到站点,因为WebBrowser只是单线程类。 我无法使用WebClient登录,因为我没有cookie。 尝试使用HttpWebRequest登录并且Iam猜测我成功登录因为我得到了标题:login.success或类似的东西,但是当我检索源页面时它返回登录页面显示我没有登录。 我想尝试登录此页面: https ://lite.betfair.com/Login.do?s = 000009z 有请求的url : https : //lite.betfair.com/SLoginsubmit.do?s = 000009z&secure = true&username = user&password = pass 码: HttpWebRequest request; HttpWebResponse response; CookieContainer cookies; string url = “https://lite.betfair.com/SLoginsubmit.do?s=000009z&secure=true&username=user&password=pass”; request = (HttpWebRequest)WebRequest.Create(url); request.AllowAutoRedirect = false; request.CookieContainer = new CookieContainer(); response = (HttpWebResponse)request.GetResponse(); cookies = request.CookieContainer; response.Close(); request = (HttpWebRequest)WebRequest.Create(“https://lite.betfair.com/Events.do?s=000209z”); request.AllowAutoRedirect = […]

在C#中使用WebClient.DownloadString发送POST

我知道有很多关于使用C#发送HTTP POST请求的问题,但我正在寻找一种使用WebClient而不是HttpWebRequest 。 这可能吗? 这很好,因为WebClient类很容易使用。 我知道我可以设置Headers属性以设置某些标头,但我不知道是否可以从WebClient实际执行POST。

如何将cookie传递给HtmlAgilityPack或WebClient?

我使用此代码登录: CookieCollection cookies = new CookieCollection(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“example.com”); request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); cookies = response.Cookies; string getUrl = “example.com”; string postData = String.Format(“my parameters”); HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl); getRequest.CookieContainer = new CookieContainer(); getRequest.CookieContainer.Add(cookies); getRequest.Method = WebRequestMethods.Http.Post; getRequest.UserAgent = “Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0”; getRequest.AllowWriteStreamBuffering = […]

WebClient非常慢

我有Webclient的问题。 这很慢。 从一个网站下载String需要大约3-5秒。 我没有任何网络问题。 这是我的Modifed WebClient。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace StatusChecker { class WebClientEx: WebClient { public CookieContainer CookieContainer { get; private set; } public WebClientEx() { CookieContainer = new CookieContainer(); ServicePointManager.Expect100Continue = false; Encoding = System.Text.Encoding.UTF8; WebRequest.DefaultWebProxy = null; Proxy = null; } public void ClearCookies() { […]