asp.net后台 怎么发送http请求?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net后台 怎么发送http请求?相关的知识,希望对你有一定的参考价值。

这个后台发送http请求有什么用?希望能帮我写个小例子

参考技术A public static HttpWebResponse GetResponseStream(string url, string method, string requestContentType, string userAgent, Dictionary<string, string> head, string data, Encoding requestEnc)
        
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.ContentType = requestContentType;
            request.Timeout = 10000;//10秒
            request.UserAgent = userAgent;
            if (head != null && head.Count > 0)
            
                foreach (var item in head)
                
                    request.Headers.Add(item.Key, item.Value);
                
            
            if (string.IsNullOrEmpty(data) == false)
            
                byte[] dataByte = requestEnc.GetBytes(data);
                request.ContentLength = dataByte.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(dataByte, 0, dataByte.Length);
                writer.Close();
            
            try
            
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return response;
            
            catch (WebException)
            
                return null;
                // throw new Exception(string.Format("message:0;status:1;url:2;data:3", ex.Message, ex.Status, url, data));
            
        
        
        public static string GetResponseText(string url, string method, string requestContentType, string userAgent, Dictionary<string, string> head, string data, Encoding requestEnc)
        
            HttpWebResponse response = GetResponseStream(url, method, requestContentType, userAgent, head, data, requestEnc);
            if (response != null)
            
                Encoding responseEnc = Encoding.UTF8;
                if (!string.IsNullOrEmpty(response.CharacterSet))
                
                    responseEnc = Encoding.GetEncoding(response.CharacterSet);
                
                StreamReader sr = new StreamReader(response.GetResponseStream(), responseEnc);
                return sr.ReadToEnd();
            
            return "";
        

追问

麻烦给我讲讲他的作用好吗?谢谢!

从 Angular 向 Asp.net 发送 post 请求时出现错误 405

【中文标题】从 Angular 向 Asp.net 发送 post 请求时出现错误 405【英文标题】:Error 405 when sending post request from Angular to Asp.net 【发布时间】:2019-04-20 05:46:12 【问题描述】:

我正在尝试从 Angular 前端向 ASP.net 后端发送发布请求。

获取请求工作正常。

当我发送时:

this.http.post("http://localhost:3000/api/Cards", "一些数据", httpOptions).subscribe(res => this.display(res));

对于我从 VS2017 运行的 ASP.NET API,我得到:

ERROR …​error: Object 消息:“请求的资源不支持 http 方法 'POST'。” ​headers: Object normalizedNames: Map(0),lazyUpdate: null,lazyInit:lazyInit() ​消息:“http://localhost:3000/api/Cards 的 Http 失败响应:405 Method Not Allowed”​名称:“HttpErrorResponse”​ok:false ​status: 405​statusText: "Method Not Allowed"​url: "http://localhost:3000/api/Cards"​: Object constructor: HttpErrorResponse() core.js:12584

对于 Angular,“httpOptions”包括:

常量 httpOptions = 标头:新的 HttpHeaders( “内容类型”:“文本/纯文本”, “接受”:“文本/纯文本” );

我的 WebApi.Config:

<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Headers" value="Content-type"/>
        <add name="Access-Control-Allow-Methods" value="POST"/>
        <add name="Access-Control-Allow-Origin" value="*"/>
      </customHeaders>
    </httpProtocol>
    <handlers>
      <remove name="WebDAV"/>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=<REMOVED>"/>
      <add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <!--<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />-->
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="TelemetryCorrelationHttpModule" />
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
      <remove name="WebDAVModule"/>
      <remove name="UrlRoutingModule"/>
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
      </dependentAssembly>      
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

它可能看起来很混乱,因为我尝试了许多不同的 Stack Overflow 问题和 asp.net 论坛帖子的解决方案。似乎没有什么能摆脱 405 错误。

WebApiConfig.cs:

public static class WebApiConfig

    public static void Register(HttpConfiguration config)
    
        // Web API configuration and services

        config.EnableCors();
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/controller/id",
            defaults: new  id = RouteParameter.Optional 
        );
    

控制器:

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class CardsController : ApiController
    
        // Test data set
        private Card[] cards = new Card[]
        
            new Card Id = 1, Suit = "D", Value = "7",
            new Card Id = 1, Suit = "D", Value = "K",
            new Card Id = 1, Suit = "S", Value = "A",
            new Card Id = 1, Suit = "H", Value = "2",
            new Card Id = 1, Suit = "S", Value = "10"
        ;

        // GET: api/Cards
        [ResponseType(typeof(IEnumerable<Card>))]
        public IEnumerable<Card> Get()
        
            return cards;
        

        // GET: api/Cards/5
        public string Get(int id)
        
            return "value";
        


         // POST: api/Cards
         public HttpResponseMessage Post(string value)
         
             return Request.CreateResponse("You found me!");
         


        // PUT: api/Cards/5
        public void Put(int id, [FromBody]string value)
        
        

        // DELETE: api/Cards/5
        public void Delete(int id)
        
        
    

如果我遗漏了任何重要内容,请告诉我。

【问题讨论】:

尝试将[HttpPost] 置于Post 操作方法之上。 这是我的新发布方法,但我仍然收到 405 错误。 [HttpPost] public HttpResponseMessage Post(string value) return Request.CreateResponse("你找到我了!"); 调用时会发生什么:this.http.post("http://localhost:3000/api/Cards", value: "some data" , httpOptions).subscribe(res =&gt; this.display(res)); 由于您正在发布,它可能会将“一些数据”序列化为与端点上的字符串值不匹配的对象。 我把它改成了上面的,仍然得到 405 错误。我很确定我的请求没问题,我用ptsv2.com 对其进行了测试。不过还是谢谢。 【参考方案1】:

您应该在 Asp.net 的端点上添加 [HttpPost],如下所示:

[HttpPost] public ActionResult MyAction()

【讨论】:

【参考方案2】:

你需要告诉控制器它是一个帖子 在动作上方添加声明。

[HttpPost]
public ActionResult MyAction()

在您的控制器操作之上。

【讨论】:

【参考方案3】:

我改用 Ruby on Rails 作为后端。

【讨论】:

以上是关于asp.net后台 怎么发送http请求?的主要内容,如果未能解决你的问题,请参考以下文章

从 Angular 向 Asp.net 发送 post 请求时出现错误 405

将正确的标头值发送到从角度 $http 到 asp.net webapi 的 Preflight 请求时出错

如何使用ASP.Net发送HTTP请求并且获取返回的XML

asp.net 发送http get请求,然后取返回来的文件或者字符串

无法使用javascript向ASP.NET web api Controller发送http POST请求

深入了解asp.net框架。生命周期以及事件处理机制