【问题标题】:c# .net access raw json request in asmx servicec# .net 访问 asmx 服务中的原始 json 请求
【发布时间】:2012-06-22 19:48:32
【问题描述】:

我正在尝试设置一个简单的代理服务器,我们将数据发布到我的代理服务器。代理服务器会将发布的数据转发到实际服务器并从实际服务器获取响应。然后在发出请求的网站读取的代理服务器上显示响应并对数据执行任何操作。 我在获取来自网站的原始帖子数据的第一部分遇到了麻烦。看起来 asmx 文件总是想根据参数做事,但我的代理只想转发原始请求。它不知道参数。以下是对代理服务器的示例请求: 本地主机/mobile.asmx 邮政 {"userName":"fake@email.com","password":"xxxx","appID":"2302FF64-925D-4E0E-B086-73AA9FF152D8"}

再一次,我不想只获取用户名和密码。我想捕获完整的原始请求并将其转发到真实服务器。

我已经尝试了很多东西。因为没有请求参数我不能使用请求。我也相信 GETUSERTOKENLOGIN 函数发生在读取原始帖子数据流之后,因此我不能再使用流来获取数据。我已经尝试了很多东西。

如果可能的话,我希望这是一个超级简单的脚本。下面是我的超级简单的例子。我知道我可以在数据周围添加一个包装器,但我不想这样做。

任何帮助将不胜感激。 MOBILE.ASMX

<%@ WebService Language="C#" Class="mobile" %>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Net;
using System.IO;
using System.Web.Script.Services;
using System.Text;
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class mobile : System.Web.Services.WebService 
{
    public mobile()
    {


    }

    // The HelloWorld() example service returns the string Hello World.
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetUserTokenLogin()
    {
        // Create a new request to the mentioned URL.
        WebRequest myWebRequest = WebRequest.Create("http://api.geonames.org/citiesJSON");
        myWebRequest.Method = "POST";
        Stream dataStream = myWebRequest.GetRequestStream();
        WebResponse myWebResponse = myWebRequest.GetResponse();

        // Print the  HTML contents of the page to the console.
        Stream streamResponse = myWebResponse.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        Char[] readBuff = new Char[256];
        int count = streamRead.Read(readBuff, 0, 256);
        String FullData = "";
        while (count > 0)
        {
            String outputData = new String(readBuff, 0, count);
            FullData = FullData + outputData;
            count = streamRead.Read(readBuff, 0, 256);
        }

        // Close the Stream object.
        streamResponse.Close();
        streamRead.Close();

        myWebResponse.Close();
        return FullData;
    }
}

【问题讨论】:

    标签: c# .net json proxy asmx


    【解决方案1】:

    要在您的 ASMX 中获取请求的原始 JSON(例如,您已从 AngularJS 向它发出 POST 请求),请尝试以下代码:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Calculate()
    {
        HttpContext.Current.Request.InputStream.Position = 0;
        var jsonString = new StreamReader(HttpContext.Current.Request.InputStream, Encoding.UTF8).ReadToEnd();
        var json = JObject.Parse(jsonString);
    
        // your code
    }
    

    请注意,这里不需要using 语句,因为您不是处理InputStream 的人。

    【讨论】:

    • @user3638471:哦,伙计!我花了几天时间寻找这个!这 3 行代码救了我。
    【解决方案2】:

    解析请求是应用服务器的工作。

    您的代理服务器上不应有任何应用程序代码。 如果您只想使用 IIS 作为代理服务器,请参考:Setting up IIS as a reverse proxy

    如果您只想要原始请求并想写出原始响应,您可以创建一个自定义 HttpModule

    参考:Creating a custom HttpModule

    在此模块中,您可以在客户端发送请求时获取请求,并将其转发到另一台服务器,然后从该服务器获取响应并将其转发给您的客户端。 (实际上你是在做一个反向代理。)

    您无法在 asp.net 网络服务中实现这一点。

    【讨论】:

    • 有没有办法从 asmx 文件中获取原始帖子?我只是在构建文件。我希望我的客户不必经历所有这些配置。是否可以将原始帖子发送到服务,以便我可以将其转发到服务器。如果我能够做到这一点,那么我的脚本就可以正常工作。
    • 我编辑了我的回复,原始帖子需要你实现IHttpModule。看看吧。
    • 谢谢。为了你的时间。我发现这个解决方案非常有用:codeproject.com/Articles/31329/… 我所要做的就是下载演示,它为您提供了一个 dll 和一个 webconfig。我将在我原来的问题中提供更准确的信息。谢谢
    【解决方案3】:

    我最终从下面下载了演示:http://www.codeproject.com/Articles/31329/Simple-Reverse-Proxy-in-C-2-0-description-and-depl 它为您提供了一个 bin 文件夹,我只是将它放在我的根目录中。比我创建了一个名为 mobile.asmx 的文件夹。在那个文件夹中,我将演示中的 webconfig 放入其中,只更改了远程服务器

    <appSettings>
     <add key="RemoteWebSite" value="http://my-clients-server.com/mobile_dev/" />
    </appSettings>
    
    <system.web>
      <httpHandlers>
       <add verb="*" path="*" type="ReverseProxy.ReverseProxy, ReverseProxy"/>
     </httpHandlers>
    </system.web>
    

    因此,只要网站请求当前域,例如:www.currentdomain.com/mobile.asmx/MYSERVICE,它就会将该请求转发到远程网站,如下所示:http://my-clients-server.com/mobile_dev/mobile.asmx/MYSERVICE

    我用我需要的 XML 和 JSON 测试了上述内容。

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 2011-09-15
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2020-07-19
      • 2014-06-05
      相关资源
      最近更新 更多