2012-06-22 61 views
1

我试图设置一个简单的代理服务器,将数据发布到我的代理服务器。代理服务器将把发布的数据转发给实际的服务器,并从实际的服务器获得响应。然后在发出请求的网站所读取的代理服务器上显示响应并对数据执行任何操作。 我在第一部分获取来自网站的原始发布数据时遇到了问题。看起来asmx文件总是希望将参数从参数中删除,但我的代理只想转发原始请求。它不知道这些参数。下面是一个例子请求到代理服务器: 本地主机/ mobile.asmx POST { “的userName”: “[email protected]”, “密码”: “XXXX”, “APPID”:“2302FF64-925D-4E0E -B086-73AA9FF152D8“}c#.net访问asmx服务中的原始json请求

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

我试过了很多东西。因为没有请求参数我不能使用请求。我也相信函数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; 
    } 
} 

回答

0

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

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

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

参见:Creating a custom HttpModule

在这个模块中,你可以得到请求的客户端发送它,并将其转发到另一台服务器,然后采取从该服务器的响应,并将其转发到客户端。 (实际上你正在制作一个反向代理服务器。)

在asp.net webservice中没有办法达到这个目的。

+0

有什么办法从asmx文件中获取原始文章?我只是建立文件。我宁愿我的客户不必经历所有的配置。是否有可能将原始文章发布到服务中,以便将其转发到服务器。如果我只是能够做到这一点,那么我的脚本就可以正常工作。 –

+0

我编辑了我的回复,原帖需要你实现'IHttpModule'。看一看。 – nunespascal

+0

谢谢。为你的时间。我发现这个解决方案非常有用: http://www.codeproject.com/Articles/31329/Simple-Reverse-Proxy-in-C-2-0-description-and-depl 我只需要下载演示给你一个dll和一个webconfig。我会在我原来的问题中提供更确切的信息。谢谢 –

0

我结束了从下面下载演示: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测试了我的需求。

0

为了让您的ASMX请求的原始JSON(例如,你做一个POST请求将其从AngularJS),试试下面的代码:

[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