2013-01-03 43 views
1

我有一個接受的MessageID和狀態從外部服務器查詢字符串web應用程序。我正在將webapp遷移到新的服務器,但我需要新的服務器將QueryString轉發到舊服務器,以防舊服務器仍在等待更新,直到我可以遷移客戶端。轉發查詢字符串到另一個服務器

外部網站叫我的webapp與MSGID = 12345678 &響應= 0

如:

http://dlrnotify.newserver.com/GetResponse.aspx?MSGID=12345678&RESPONSE=0 

我需要我的身後GetResponse.aspx代碼在本地處理的消息,然後轉發請求到舊服務器。例如,美其名曰:

http://dlrnotify.oldserver.com/GetResponse.aspx?MSGID=12345678&RESPONSE=0 

我真的不希望將用戶重定向到舊的Web服務器,只是從我的應用程序通過查詢字符串上。

我可以通過調用Response.QueryString.ToString()我只需要知道如何張貼到舊服務器,而不擾亂任何事情的查詢字符串。

很抱歉,如果這是一個愚蠢的問題,我不和Web應用程序經常工作,我明明使用了錯誤的搜索條件。

回答

2

您可以使用HttpWebRequest和HttpWebResponse這一點。下面是一個例子使用有關如何使用HttpWebRequest的

Uri uri = new Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312"); 
    string data = "field-keywords=ASP.NET 2.0"; 
    if (uri.Scheme == Uri.UriSchemeHttp) 
    { 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
     request.Method = WebRequestMethods.Http.Post; 
     request.ContentLength = data.Length; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
     writer.Write(data); 
     writer.Close(); 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 
     string tmp = reader.ReadToEnd(); 
     response.Close(); 
     Response.Write(tmp); 
    } 
0

在新服務器上執行代碼(處理消息)後,POST數據到遠程網頁thses

Uri uri = new Uri("http://www.microsoft.com/default.aspx"); 
    if(uri.Scheme = Uri.UriSchemeHttp) 
    { 
     HttpWebRequest request = HttpWebRequest.Create(uri); 
     request.Method = WebRequestMethods.Http.Get; 
     HttpWebResponse response = request.GetResponse(); 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 
     string tmp = reader.ReadToEnd(); 
     response.Close(); 
     Response.Write(tmp); 
} 

示例代碼,手動生成HttpWebRequest這應該是你的舊服務器與你已經想出來形成相同的查詢字符串。

0

我有一個任務與您的帖子相同。但是還有一點。 由於我們有兩個Web應用程序,一個在asp.net中,另一個在PHP中。我們都在創建用戶配置文件。現在的任務是在Asp.NET應用程序中創建用戶,我們需要從Asp.Net應用程序中將相同的信息保存在PHP應用程序中。

我使用爲下面的代碼,但它不是wroking,能否請您看看吧,讓我知道我錯過了什麼。

 CookieContainer cookies = new CookieContainer(); 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://localhost/admin/config/popup_user_info_brand.php"); 
     request.PreAuthenticate = true; 
     request.AllowWriteStreamBuffering = true; 
     request.CookieContainer = cookies; // note this 
     request.Method = "POST"; 

     string boundary = System.Guid.NewGuid().ToString(); 
     string Username = "admin"; 
     string Password = "admin"; 

     if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)) 
     { 
      request.Credentials = new NetworkCredential(Username, Password); 
      request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); 

      StringBuilder sb = new StringBuilder(); 

      sb.AppendLine("Content-Disposition: form-data; name=\"name\""); 
      sb.AppendLine("Singh"); 

      sb.AppendLine("Content-Disposition: form-data; name=\"username\""); 
      sb.AppendLine("Singh123"); 

      sb.AppendLine("Content-Disposition: form-data; name=\"email\""); 
      sb.AppendLine("[email protected]"); 

      sb.AppendLine("Content-Disposition: form-data; name=\"password\""); 
      sb.AppendLine("[email protected]"); 

      // This is sent to the Post 
      byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString()); 

      request.ContentLength = bytes.Length; 

      using (Stream requestStream = request.GetRequestStream()) 
      { 
       requestStream.Write(bytes, 0, bytes.Length); 
       requestStream.Flush(); 
       requestStream.Close(); 

       using (WebResponse response = request.GetResponse()) 
       { 
        using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
        { 
         HttpContext.Current.Response.Write(reader.ReadToEnd()); 
        } 
       } 
      } 
     } 

注意: - PHP網站是第三方,我們無法訪問代碼。

感謝, 海金妮。

相關問題