2012-06-22 138 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