Click here to Skip to main content
15,917,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can any one help me on this like i wants to access one site to another by passing regarding values and also i tried the below code that shows below

C#
using System;
using System.Web;  // for HttpUtility class
using System.Threading;
using System.Text; // for Encoding class
using System.Net;  // for HttpWebRequest class
using System.IO;   // for StreamReader class

namespace sampleexe
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
    {
            
        string url = "http://localhost:3974/Default.aspx";


      string viewstate = InitialViewState(url);

      viewstate = HttpUtility.UrlEncode(viewstate);
      string data = "TextBox1=red&TextBox2=empty&Button1=clicked&__VIEWSTATE="+viewstate;
      byte[] buffer = Encoding.UTF8.GetBytes(data);
      string proxy = null;

      WebRequest req = WebRequest.Create(url);
      req.Method = "POST";
      req.ContentType = "application/x-www-form-urlencoded";
      req.ContentLength = buffer.Length;
      req.Proxy = new WebProxy(proxy, true); // ignore for local addresses
      

      Stream reqst = req.GetRequestStream(); // add form data to request stream
      reqst.Write(buffer, 0, buffer.Length);
      reqst.Flush();
      reqst.Close();

      Console.WriteLine("\nPosting 'red' to " + url);
      try
      {
          
          req.Credentials = CredentialCache.DefaultCredentials;
         // HttpWebResponse response = (HttpWebResponse)req.GetResponse();
          HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // send request,  get response
          Console.WriteLine("\nResponse stream is: \n");
          Stream resst = res.GetResponseStream(); // display HTTP response
          StreamReader sr = new StreamReader(resst);
          Console.WriteLine(sr.ReadToEnd());
          sr.Close();
          resst.Close();
      }
      catch (System.Net.WebException ex)
      {
          String responseFromServer = ex.Message.ToString() + " ";
          if (ex.Response != null)
          {
              using (WebResponse response = ex.Response)
              {
                  Stream data11 = response.GetResponseStream();
                  using (StreamReader reader = new StreamReader(data11))
                  {
                      responseFromServer += reader.ReadToEnd();
                  }
              }
          }
          Console.WriteLine(responseFromServer);

      } 
      
  
      Console.WriteLine("\nDone");
      Console.ReadLine();
    } // Main()

        static string InitialViewState(string url)
        {
            WebClient wc = new WebClient();
            Stream st = wc.OpenRead(url);
            StreamReader sr = new StreamReader(st);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                if (line.IndexOf("__VIEWSTATE") != -1) // found line
                {
                    sr.Close();
                    st.Close();
                    int startIndex = line.IndexOf("value=") + 7;
                    int endIndex = line.IndexOf("\"", startIndex);
                    int count = endIndex - startIndex;
                    return line.Substring(startIndex, count);
                }

            }
            sr.Close();
            st.Close();
            return "not found";
        } // InitialViewState()

    } // Class1
} // ns PostToASPdotNET



here the error i'm getting is "The remote server returned an error: (500) Internal Server Error.System.Net.HttpWebRequest.GetResponse()"

Please help me on this to resolve!
Posted
Updated 29-Dec-11 0:21am
v2

1 solution

VIEWSTATE is encrypted to prevent any malicious manipulation and wouldn't be valid on any other page or site.
 
Share this answer
 
Comments
visnumca123 29-Dec-11 8:36am    
Can you explain me clearly!

if i removed that viewstate process means it will work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900