如何从带有参数的Classic ASP发送HTTP POST到WEB API? [英] How to send a HTTP POST from Classic ASP with a parameter to a WEB API?

查看:86
本文介绍了如何从带有参数的Classic ASP发送HTTP POST到WEB API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在另一个端口的同一台计算机上创建对WEB API的调用.它工作正常并发送回字符串,并命中.NET断点,但参数从未传递过..(它为null)..在经典的ASP代码中我缺少通过该字符串传递的东西吗? (DataToSend)

I am trying to create a call to a WEB API on the same machine on another port. It works fine and sends back the string and hits the .NET breakpoint but the parameter is never being passed..(it is null) .. Is there something I am missing in the classic ASP code to pass that string ? (DataToSend)

我的呼叫代码:

  <%
      Response.Buffer = True
      Dim xml
     ' Set xml = Server.CreateObject("Microsoft.XMLHTTP")
    Set xml = server.Createobject("MSXML2.XMLHTTP")

     DataToSend="<?xml version=""1.0"" encoding=""UTF-8""?><codes sku=""123123"" num-codes=""234234"" />"

      xml.Open "POST", _
          "http://localhost:1303/api/RegistrationCode", _
          False

      xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
      xml.setRequestHeader "X-VitalSource-API-Key", "xxx"

      xml.Send DataToSend

      'Display the HTML both as HTML and as text
      Response.Write "<h1>The HTML text</h1><xmp>"
      Response.Write xml.responseText
      Response.Write "</xmp><p><hr><p><h1>The HTML Output</h1>"
      Response.Write xml.responseText
      Set xml = Nothing
%>

WEB API代码:

WEB API code:

   public class RegistrationCodeController : ApiController
{
    string testXmlString = "<SomeValue>6</SomeValue>";



       public string Post([FromBody]string value)
    {
        return testXmlString;
    }

}

推荐答案

我也遇到了这个问题.答案很晚,但是...

I ran into this issue as well. Late answer, but...

我遇到一种情况,因为它很大并且超出了大小限制,所以无法在URL中发送数据,因此我不得不使用POST,而不是GET.经过反复试验,这就是我所做的:

I had a case where I could not send the data in the URL because it was very large and exceeded the size limit, so I had to use POST, not GET. After some trial and error this is what I did:

在经典的ASP文件中

Public Function PostData(link, data)
    on error resume next

    if link<>"" then
        data = "{'Name': '" & data & "'}"
        data = Replace(data, "'", """")

        Dim oXMLHTTP
        Set oXMLHTTP = CreateObject("Msxml2.XMLHTTP.3.0")
        if oXMLHTTP is nothing then Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")

        oXMLHTTP.Open "POST", link, False
        oXMLHTTP.setRequestHeader "Content-Type", "application/json"
        oXMLHTTP.send data

        If oXMLHTTP.Status = 200 Then
            PostData = oXMLHTTP.responseText
        Else
            response.Write "Status: " & oXMLHTTP.Status & " | "
            response.Write oXMLHTTP.responseText
            response.end
        End If

    end if
End Function

在我的WebAPI函数中:

In my WebAPI function:

public class PayLoad
{
    public string Name { get; set; }
}

[Route("api/Values/PostUnidataMethod")]
[HttpPost]
public string PostUnidataMethod([FromBody]PayLoad data)
{
    return data.Name;
}

我对ASP不太熟悉,但是我发现了一些示例,所有示例都使用JSON将数据发送到Web API函数,而不仅仅是一个字符串.因此,我创建了一个简单的DTO,然后将数据制成JSON字符串.请记住将单引号转换为双引号.我喜欢 JSONLint 来测试数据以确保结果.

I am not very familiar with ASP, but I found several examples that all used JSON to send the data to the Web API function, rather than just a string. So I created a simple DTO and then made the data into a JSON string. Remember to convert single-quotes to double-quotes. I like JSONLint to test the data to make sure.

这篇关于如何从带有参数的Classic ASP发送HTTP POST到WEB API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆