对于c#–System.Net.Http.WebRequestHandler源代码在哪里?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解c#获取网页源码,并且为您提供关于.NETHttpW
对于c# – System.Net.Http.WebRequestHandler源代码在哪里?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解c#获取网页源码,并且为您提供关于.NET HttpWebRequest应用、.NET System.Web.HttpContext.Current.Request报索引超出数组界限。、.net – 什么是System.Web.Mvc.MvcHandler.ProcessAsyncRequest()?、.net-4.0 – 从自定义IHttpHandler调用MvcHttpHandler.ExecuteRequest时出错的宝贵知识。
本文目录一览:- c# – System.Net.Http.WebRequestHandler源代码在哪里?(c#获取网页源码)
- .NET HttpWebRequest应用
- .NET System.Web.HttpContext.Current.Request报索引超出数组界限。
- .net – 什么是System.Web.Mvc.MvcHandler.ProcessAsyncRequest()?
- .net-4.0 – 从自定义IHttpHandler调用MvcHttpHandler.ExecuteRequest时出错
c# – System.Net.Http.WebRequestHandler源代码在哪里?(c#获取网页源码)
The version of the framework that we currently have indexed is .NET framework version 4.5.1.
然而,当我去那里看看HttpClient代码,我得到的是存根,我找不到任何代码,使其运行像System.Net.Http.WebRequestHandler的位.
我在哪里可以找到这个代码,为什么还没有包含这个代码?
解决方法
“WebRequestHandler不包括在System.Net.Http DLL中,而是在System.Net.Http.WebRequest DLL中,因此您必须明确地将其作为参考,以便查看它.
.NET HttpWebRequest应用
提供基于HttpWebRequest的请求的应用类,其中包含:get请求(带参或不带参)、post请求、文件传输请求
方法的具体说明:
PostHttp:post请求,支持三种提交模式:FROM、JSON、XML
GetHttp:get请求(带参或不带参)
PostFile:文件传输请求
/// <summary>
/// 提供HttpWebRequest请求的相关封装.
/// 本接口所提供的方法均不含异常拦截处理,请在调用的主方法中去拦截请求异常.
/// </summary>
public static class Request
{
#region post 请求
public enum PostType
{
/// <summary>
/// 表单模式,传入参数格式如:roleId=1&uid=2
/// </summary>
FROM = 0,
/// <summary>
/// JSON格式字符串,格式如:{k:v,k2:v2,k3:{kk1:vv1}}
/// </summary>
JSON = 1,
/// <summary>
/// XML模式
/// </summary>
XML = 2
}
public static string PostHttp(string url, string body, PostType type = PostType.FROM)
{
string resStr = string.Empty;
switch (type)
{
case PostType.FROM:
resStr = PostForm(url, body);
break;
case PostType.JSON:
resStr = PostJson(url, body);
break;
case PostType.XML:
resStr = PostXml(url, body);
break;
default:
resStr = PostForm(url, body);
break;
}
return resStr;
}
#region post请求几种方式(私有)
/// <summary>
/// POST表单
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
private static string PostForm(string url, string body)
{
string resStr = string.Empty;
byte[] bs = Encoding.UTF8.GetBytes(body);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = bs.Length;
using (Stream reqStream = myRequest.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
resStr = sr.ReadToEnd();
sr.Close();
}
myRequest.Abort();
return resStr;
}
/// <summary>
/// POST XML
/// </summary>
/// <param name="url">请求url(不含参数)</param>
/// <param name="body">请求body. soap"text/xml; charset=utf-8"xml字符串</param>
/// <returns></returns>
private static string PostXml(string url, string body)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/xml;charset=utf-8";
httpWebRequest.Method = "POST";
//httpWebRequest.Timeout = timeout;//设置超时
httpWebRequest.Headers.Add("SOAPAction", "http://tempuri.org/mediate");
byte[] btBodys = Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
#region 取消异常拦截
//HttpWebResponse httpWebResponse;
//try
//{
// httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//}
//catch (WebException ex)
//{
// httpWebResponse = (HttpWebResponse)ex.Response;
//}
#endregion
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
return responseContent;
}
/// <summary>
/// POST json
/// </summary>
/// <param name="url"></param>
/// <param name="JSONData"></param>
/// <returns></returns>
private static string PostJson(string url, string JSONData)
{
string result = string.Empty;
//byte[] bs = Encoding.UTF8.GetBytes(body);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/json";
using (var streamWriter = new StreamWriter(myRequest.GetRequestStream()))
{
streamWriter.Write(JSONData);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)myRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
httpResponse.Close();
myRequest.Abort();
return result;
}
#endregion
#endregion
#region get请求
/// <summary>
/// get请求
/// </summary>
/// <param name="url">请求url(不含参数)</param>
/// <param name="postDataStr">参数部分:roleId=1&uid=2</param>
/// <param name="timeout">等待时长(毫秒)</param>
/// <returns></returns>
public static string GetHttp(string url, string postDataStr,int timeout=2000)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.Timeout = timeout;//等待
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
request.Abort();
return retString;
}
#endregion
#region 文件传输请求
/// <summary>
/// 传输文件到指定接口
/// </summary>
/// <param name="url"></param>
/// <param name="filePath">文件物理路径</param>
/// <returns></returns>
public static string PostFile(string url, string filePath)
{
string resStr = string.Empty;
// 初始化HttpWebRequest
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
// 封装Cookie
Uri uri = new Uri(url);
Cookie cookie = new Cookie("Name", DateTime.Now.Ticks.ToString());
CookieContainer cookies = new CookieContainer();
cookies.Add(uri, cookie);
httpRequest.CookieContainer = cookies;
if (!File.Exists(filePath))
{
return "文件不存在";
}
FileInfo file = new FileInfo(filePath);
// 生成时间戳
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes(string.Format("\r\n--{0}--\r\n", strBoundary));
// 填报文类型
httpRequest.Method = "Post";
httpRequest.Timeout = 1000 * 120;
httpRequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
// 封装HTTP报文头的流
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append(Environment.NewLine);
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(file.Name);
sb.Append("\"");
sb.Append(Environment.NewLine);
sb.Append("Content-Type: ");
sb.Append("multipart/form-data;");
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
// 计算报文长度
long length = postHeaderBytes.Length + file.Length + boundaryBytes.Length;
httpRequest.ContentLength = length;
// 将报文头写入流
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new byte[4096];
int bytesRead = 0;
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
// 将报文尾部写入流
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
// 关闭流
requestStream.Close();
using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse())
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
resStr = sr.ReadToEnd();
sr.Close();
//Console.WriteLine("反馈结果" + responseString);
}
httpRequest.Abort();
return resStr;
}
#endregion
}
.NET System.Web.HttpContext.Current.Request报索引超出数组界限。
移动端使用Dio发送 FormData, 请求类型 multipart/form-data
, FormData内可以一个或多个包含文件时。
请求接口时获取上传的fomdata数据使用 System.Web.HttpContext.Current.Request 接受时 报索引超出数组界限问题。
使用MultipartFormDataStreamProvider接受数据不受影响。
解决过程不多说。说多了都是泪。下面是解决方案
解决方法:
1.修改IIS的applicationhost.config
a.文件位置: %windir%/system32/inetsrv/config/applicationhost.config
b.找到 <requestFiltering> 节点
c.然后找到<serverRuntime />此项。如果设置了uploadReadAheadSize的大小 则删除掉就可以了。
附加知识点:
iis7上传文件大小是有限制的。根据自己需求来进行设置相对应的处理
1.web.config中添加如下内容
<configuration>
<system.web>
<httpRuntime maxRequestLength="上传大小的值(单位:byte)" executionTimeout="120"/>
</system.web>
</configuration>
2.web.config中,把以下内容加在<system.webServer>节点
<security>
<requestFiltering >
<requestLimits maxAllowedContentLength="上传大小的值(单位:KB)" >
</requestLimits>
</requestFiltering>
</security>
.net – 什么是System.Web.Mvc.MvcHandler.ProcessAsyncRequest()?
此函数调用可能需要300毫秒到100秒(严重的是100秒).我试图搜索msdn文档,但http://msdn.microsoft.com/en-us/library/system.web.mvc.mvchandler.aspx上没有任何内容
显然,这里有些东西在骗我.
我有一些理论说明为什么这么长时间:
>类型推断?我正在使用结构图.
>服务器资源问题?
> .net版本不兼容某种?
> asp.net mvc某种不兼容性?
环境:
.net 4
asp.net mvc 3
专用的vm
解决方法
因此,我认为它可能是New Relic注入的东西,或者正如你所说的那样,是一种红鲱鱼,有些东西对我们说谎!我关闭了New Relic,并与他们的支持团队联系.
今天,在收到来自New Relic团队的一位反应灵敏且彬彬有礼的成员的几封电子邮件之后,他们又回到我身边并确认这是一个错误(各种各样).以下是他们的回复:
ProcessAsyncRequest is a custom name that we use for any metric being
recorded that is not / does not inherit from “System.Web.UI.Page.”
Given that MVC view engine uses “System.Web.Mvc.ViewPage” all of those
metrics will incorrectly fall under the New Relic moniker of
“ProcessAsyncRequest.”I will be working on a modification to the agent and the core
instrumentation that will hopefully aggregate these metrics
appropriately. I am sorry for the confusion and trouble this has
caused you.I’ll give you an update as I get close to a solution.
编辑:下面的New Relic的进一步回复 – 看起来他们有一个修复到位.
I just pushed a commit that will help us better classify the
transactions coming from the installed agent.As far as the performance issue we did discover an issue reported by
the awesome engineers at AppHarbor that was causing TypeLoadExceptions
which might be related to slow loading / compiling code being put into
the cache. We have found the cause and are in the final testing phases
of that fix and we are hoping to get the fix in the next release of
the agent.
来自New Relic的尼克非常出色地回应了这一点,他们的产品非常有用,所以我没有任何不好的感受,只是想我会在这里分享细节.
很高兴发现我的MVC应用程序中没有鬼魂!
现在,我对任何有这些问题的人的建议是关闭New Relic直到他们的下一个版本.
编辑2:New Relic的Nick今天通过电子邮件发送给我 – 他们最新的代理(版本2.0.9.15) – 现在已经可以使用,应该可以解决这个问题.
.net-4.0 – 从自定义IHttpHandler调用MvcHttpHandler.ExecuteRequest时出错
它在asp.net MVC2中运行良好,但在我使用IISExpress 7.5将代码迁移到MVC4之后,我开始在行上获得InvalidOperationException:
httpHandler.ProcessRequest(HttpContext.Current);
有消息:
‘HttpContext.SetSessionStateBehavior’ can only be invoked before
‘HttpApplication.AcquireRequestState’ event is raised.
ASP.NET Development Server不会出现任何问题.
有谁知道这里发生了什么,以及如何解决它?
解决方法
看到这个问题:MVC3 Application Inside Webforms Application Routing is throwing a HttpContext.SetSessionStateBehavior Error in IIS7.5
关于c# – System.Net.Http.WebRequestHandler源代码在哪里?和c#获取网页源码的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.NET HttpWebRequest应用、.NET System.Web.HttpContext.Current.Request报索引超出数组界限。、.net – 什么是System.Web.Mvc.MvcHandler.ProcessAsyncRequest()?、.net-4.0 – 从自定义IHttpHandler调用MvcHttpHandler.ExecuteRequest时出错等相关知识的信息别忘了在本站进行查找喔。
本文标签: