2016-03-18 79 views
0

我做了這個webservice中繼請求到另一臺服務器。它接受一個請求,將其發送到它自己的url,然後將請求發送到另一個url並將結果反饋給它的客戶端。請求中繼webservice沒有登錄

void Application_BeginRequest(object sender, EventArgs e) 
{ 
    // Setup destination url schemes 
    string newAuth = "localhost:1861"; 
    string newUrl = "http://" + newAuth + Request.Url.PathAndQuery; 

    // Setup the request from this server to the other server 
    HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(newUrl); 
    newRequest.AllowAutoRedirect = false; 

    // Copy all needed headers 
    List<string> copyHeaderNames = new List<string>() 
    { 
     "Accept-Encoding", 
     "Accept-Language", 
     "Upgrade-Insecure-Requests", 
     "Cache-Control", 
     "Connection", 
     "Cookie" 
    }; 
    foreach (var key in copyHeaderNames) 
    { 
     try 
     { 
      if (newRequest.Headers.AllKeys.Contains(key)) 
      { 
       newRequest.Headers[key] = Request.Headers[key].Replace(Request.Url.Authority, newAuth); 
      } 
      else 
      { 
       newRequest.Headers.Add(key, Request.Headers[key].Replace(Request.Url.Authority, newAuth)); 
      } 
     } 
     catch { } 
    } 

    // Then setup the constant paramenters of the new request 
    newRequest.KeepAlive = Request.Headers["Connection"] == "keep-alive"; 
    newRequest.Accept = Request.Headers["Accept"]; 
    newRequest.Expect = Request.Headers["Expect"]; 
    newRequest.UserAgent = Request.Headers["User-Agent"]; 

    newRequest.ContentType = Request.ContentType; 
    newRequest.Method = Request.HttpMethod; 
    newRequest.Host = newAuth; 
    newRequest.Referer = newUrl; 

    // If the request is a POST, I need to copy the inputstream. 
    if (Request.HttpMethod == "POST") 
    { 
     byte[] inputBytes = ReadToByteArray(Request.InputStream); 
     string inputString = System.Text.Encoding.Default.GetString(inputBytes); 

     // Replace original url with destination url 
     inputString = inputString.Replace(Request.Url.Authority, newAuth); 
     inputBytes = System.Text.Encoding.Default.GetBytes(); 

     Stream reqStream = newRequest.GetRequestStream(); 
     reqStream.Write(inputBytes, 0, inputBytes.Length); 
     reqStream.Close(); 
    } 

    // Then do the request 
    using (var resp = (HttpWebResponse)newRequest.GetResponse()) 
    { 
     // Setup response paramenters 
     Response.StatusCode = (int)resp.StatusCode; 
     Response.StatusDescription = resp.StatusDescription; 

     // Get the response stream 
     using (var respstream = resp.GetResponseStream()) 
     { 
      var res = ReadToByteArray(respstream); 

      // And respond it in the current response 
      Response.BinaryWrite(res); 

      // Then I copy all response headers to the current response 
      foreach (var key in resp.Headers.AllKeys) 
      { 
       try 
       { 
        // Replace the destination url back to the current url       
        string value = resp.Headers[key].Replace(newAuth, Request.Url.Authority); 

        if (Response.Headers.AllKeys.Contains(key)) 
        { 
         Response.Headers[key] = value; 
        } 
        else 
        { 
         Response.Headers.Add(key, value); 
        } 
       } 
       catch { } 
      } 
     } 
    } 

    // Tell the program to end the request. 
    Response.End(); 
} 

public static byte[] ReadToByteArray(Stream input) 
{ 
    byte[] buffer = new byte[16 * 1024]; 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     int read; 
     while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      ms.Write(buffer, 0, read); 
     } 
     return ms.ToArray(); 
    } 
} 

現在一切正常,除了登錄。另一個網站是一個asp.net mvc4應用程序,它使用帶有身份驗證cookie的標準Membership。

任何想法?

回答

0

猜測複製請求對象的內容並不完全複製它。解決方案是將OSI模型下移1層到TCP層,然後在該層上進行中繼。