【问题标题】:Forward a QueryString to another Server将 QueryString 转发到另一台服务器
【发布时间】:2013-01-03 11:59:40
【问题描述】:

我有一个 webapp,它接受来自外部服务器的 messageid 和状态作为 QueryString。我正在将 webapp 迁移到新服务器,但我需要新服务器将 QueryString 转发到旧服务器,以防旧服务器仍在等待更新,直到我可以让我的客户端迁移过来。

外部网站使用 ?MSGID=12345678&RESPONSE=0 调用我的 webapp

例如:

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

我需要 GetResponse.aspx 后面的代码在本地处理消息,然后将请求转发到旧服务器。例如,调用:

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

我真的不想将用户重定向到旧的网络服务器,只是为了从我的应用程序传递查询字符串。

我可以通过调用 Response.QueryString.ToString() 来获取 QueryString,我只需要知道如何将其发布到旧服务器而不破坏任何内容。

对不起,如果这是一个愚蠢的问题,我不经常使用网络应用程序,显然我使用了错误的搜索词。

【问题讨论】:

    标签: asp.net vb.net http query-string


    【解决方案1】:

    您可以为此使用 HttpWebRequest 和 HttpWebResponse。下面是一个使用 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 将数据发布到远程网页的示例代码

       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);
       }
    

    【讨论】:

      【解决方案2】:

      在新服务器上执行您的代码(处理消息)后,手动生成一个 HttpWebRequest,它应该使用与您已经想出的相同查询字符串发送到您的旧服务器。

      【讨论】:

        【解决方案3】:

        我有一个与您的帖子相同的任务。但还有一点。 因为我们有两个 Web 应用程序,一个在 asp.net 中,另一个在 PHP 中。在两者中,我们都创建了用户配置文件。现在的任务是在 Asp.NET 应用程序中创建用户,我们需要将相同的信息从 Asp.Net 应用程序保存到 PHP 应用程序中。

        我正在使用下面的代码,但它没有工作,请你看看它,让我知道我缺少什么。

                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("a@b.com");
        
                    sb.AppendLine("Content-Disposition: form-data; name=\"password\"");
                    sb.AppendLine("P@ssword");
        
                    // 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 网站是第 3 方,我们无法访问代码。

        谢谢, 吉尼。

        【讨论】:

          猜你喜欢
          • 2012-06-07
          • 2012-04-23
          • 2012-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-10
          • 2016-01-20
          • 2017-09-05
          相关资源
          最近更新 更多