Molloch
Molloch

Reputation: 2381

Forward a QueryString to another Server

I have a webapp that accepts a messageid and status as a QueryString from an external server. I am migrating the webapp to a new server, but I need the new server to forward the QueryString to the old server in case the old server is still waiting for updates, and until I can get my clients migrated over.

The External website calls my webapp with ?MSGID=12345678&RESPONSE=0

eg:

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

I need my code behind GetResponse.aspx to process the message locally, and then forward the request to the old server. eg, calling:

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

I don't really want to redirect the user to the old web server, just to pass the querystring on from my app.

I can get the QueryString by calling Response.QueryString.ToString() I just need to know how to post that to the old server without upsetting anything.

Sorry if this is a dumb question, I don't work with web apps very often and am obviously using the wrong search terms.

Upvotes: 1

Views: 859

Answers (3)

Ginni
Ginni

Reputation: 1

I have a task which is same as your post. But there is a little more in that. As we have two web applications, one in asp.net and other in PHP. In both we create user profiles. Now the task is to Create users in Asp.NET application and we need to save the same information in PHP application from Asp.Net app.

I am using the below code for that but it is not wroking, Can you please look at it and let me know what I am missing.

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

Note:- PHP web site is a 3rd party, we have no access on code.

Thanks, Ginni.

Upvotes: 0

erichste
erichste

Reputation: 759

After executing your code (process message) on the new server, manually generate a HttpWebRequest which should be to your old server with the same querystring as you already figured out to form.

Upvotes: 0

शेखर
शेखर

Reputation: 17614

You can use HttpWebRequest and HttpWebResponse for this. Below is an example to use 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);
 }

Sample Code on how to Post Data to remote Web Page using 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);
   }

Upvotes: 2

Related Questions