.NET中WebClient的HttpWebRequest类与.NET 之间有什么区别?他们都做了非常相似的事情.实际上,为什么不将它们合并到一个类中(太多的方法/变量等可能是一个原因,但.NET中有其他类违反了该规则).
谢谢.
我之前使用过带有HttpWebRequest和HttpWebResponse会话的CookieContainer,但现在,我想将它与WebClient一起使用.据我所知,没有像HttpWebRequests(request.CookieContainer)那样的内置方法.如何从CookieContainer中的WebClient收集cookie?
public class CookieAwareWebClient : WebClient
{
private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = m_container;
}
return request;
}
}
Run Code Online (Sandbox Code Playgroud)
这是最好的方法吗?
在我看来,大部分可以完成的事情HttpWebRequest/Response也可以在WebClient课堂上完成.我读到的地方WebClient是一个高级包装器WebRequest/Response.
到目前为止,我无法看到任何无法实现的功能HttpWebRequest/Response,也无法看到WebClientHttpWebRequest/Response将为您提供更多"细粒度"控件.
我何时应该使用WebClient HttpWebRequest/Response?何时?(显然,HttpWebRequest/Response是HTTP特定的.)
如果HttpWebRequest/Response是较低级别那么WebClient,HttpWebRequest/Response我能用不能实现的目标完成WebClient什么?
如何从字符串URL获取主机域?
GetDomain有1个输入"URL",1个输出"Domain"
例1
INPUT: http://support.domain.com/default.aspx?id=12345
OUTPUT: support.domain.com
Run Code Online (Sandbox Code Playgroud)
例题
INPUT: http://www.domain.com/default.aspx?id=12345
OUTPUT: www.domain.com
Run Code Online (Sandbox Code Playgroud)
示例3
INPUT: http://localhost/default.aspx?id=12345
OUTPUT: localhost
Run Code Online (Sandbox Code Playgroud) 我正在尝试找到一种方法来在请求Https资源时忽略证书检查,到目前为止,我在互联网上找到了一些有用的文章.
但我还是有一些问题.请查看我的代码.我只是不明白代码ServicePointManager.ServerCertificateValidationCallback是什么意思.
何时调用此委托方法?还有一个问题,我应该在哪个地方编写这段代码?在ServicePointManager.ServerCertificateValidationCallback执行之前还是之前Stream stream = request.GetRequestStream()?
public HttpWebRequest GetRequest()
{
CookieContainer cookieContainer = new CookieContainer();
// Create a request to the server
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_remoteUrl);
#region Set request parameters
request.Method = _context.Request.HttpMethod;
request.UserAgent = _context.Request.UserAgent;
request.KeepAlive = true;
request.CookieContainer = cookieContainer;
request.PreAuthenticate = true;
request.AllowAutoRedirect = false;
#endregion
// For POST, write the post data extracted from the incoming request
if (request.Method == "POST")
{
Stream clientStream = _context.Request.InputStream;
request.ContentType = _context.Request.ContentType;
request.ContentLength …Run Code Online (Sandbox Code Playgroud) 我在ASP.NET中创建一个Web请求,我需要向主体添加一堆数据.我怎么做?
var request = HttpWebRequest.Create(targetURL);
request.Method = "PUT";
response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud) 我看到了这段代码:
var request = (HttpWebRequest) WebRequest.Create("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
你为什么要演员(HttpWebRequest)?为什么不用HttpWebRequest.Create?为什么HttpWebRequest.Create要做一个WebRequest,而不是一个HttpWebRequest?
我创建了一个程序,试图在网站上发布一个字符串,我收到此错误:
"服务器提交了协议违规.Section = ResponseStatusLine"
在这行代码之后:
gResponse = (HttpWebResponse)gRequest.GetResponse();
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个异常?
有没有办法使用post方法发送数据而没有表单,没有刷新页面只使用纯Javascript(不是jQuery $.post())?也许是httprequest或其他东西,现在找不到它.
我仍然是c#的新手,我正在尝试为这个页面创建一个应用程序,告诉我何时收到通知(回答,评论等等).但是现在我只是想简单地调用api来获取用户的数据.
我正在使用Visual Studio express 2012来构建C#应用程序,其中(现在)您输入了您的用户ID,因此应用程序将使用用户ID发出请求并显示此用户ID的统计信息.
这是我正在尝试发出请求的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Request library
using System.Net;
using System.IO;
namespace TestApplication
{
class Connect
{
public string id;
public string type;
protected string api = "https://api.stackexchange.com/2.2/";
protected string options = "?order=desc&sort=name&site=stackoverflow";
public string request()
{
string totalUrl = this.join(id);
return this.HttpGet(totalUrl);
}
protected string join(string s)
{
return api + type + "/" + s + options;
}
protected string get(string url)
{
try
{
string rt; …Run Code Online (Sandbox Code Playgroud) httpwebrequest ×10
c# ×8
.net ×3
webclient ×3
certificate ×1
cookies ×1
http-post ×1
javascript ×1
post ×1
ssl ×1
string ×1
uri ×1
url ×1