locked
Problem implementing a CURL equivalent in .net RRS feed

  • Question

  • User910780453 posted

    Hi all,

    I am implementing the code of a php file using CURL library in C# 2.0. 

    This is my scenario-

    1. I request a service with login id and password and it should return me a CID, in a file (main.jsp) if the credentials are validated.

    2. After getting logged in, I use this service by another url and do my stuff here.

    But when I do the first step with the code below, as I observe in Fiddler, First I get redirected to main.jsp then before getting the response back to my aspx page, I get again redirected to login page ( I get automatically logged out, before I could do step 2)

    Below is my code

     

     string strURl = @"http://wwwa.myTargetService.com/wapserv"; 
            
            string strHeader = "userid=myusername&password=mypassword&HiddenAction=loginPage";
    
            HttpWebRequest textMessageRequest = (HttpWebRequest)WebRequest.Create(strURl);
    
            textMessageRequest.Method = "POST";
            textMessageRequest.ContentType = "application/x-www-form-urlencoded";
            textMessageRequest.ContentLength = strHeader.Length;
    
            AddPostVarsToHeader(textMessageRequest, strHeader);
    
            string responseString;
            using (StreamReader streamReader = new StreamReader(textMessageRequest.GetResponse().GetResponseStream()))
            {
                // The response string has the HTML response (if its HTML)
                responseString = streamReader.ReadToEnd();
    
                string strMsg = "Message Sent: ";
                strMsg += "Note: This message can take a few minutes (to a couple hours from my testing) to arrive.";
            }
     
    private static void AddPostVarsToHeader(WebRequest request, string values)
        {
            using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream(), ASCIIEncoding.ASCII))
            {
                streamWriter.Write(values);
                streamWriter.Close();
            }
        }
    What should I do to avoid this redirect before I have done with step 2....
    Any suggestions ? 
      
    Saturday, January 24, 2009 5:55 AM

Answers

  • User910780453 posted

    I was ignorant of Cookie/Sessions exchange between server and client during the requests and responses. So added this code and found it working - 

     CookieContainer cookieC = new CookieContainer();
    //objWebRequest.CookieContainer.SetCookies(objWebRequest.RequestUri, "cookie_ws");
    objWebRequest.CookieContainer = cookieC;
    //get response for request
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 7:26 AM
  • User910780453 posted

    I was ignorant of Cookie/Sessions exchange between server and client during the requests and responses. So added this code and found it working - 

     CookieContainer cookieC = new CookieContainer();
            //objWebRequest.CookieContainer.SetCookies(objWebRequest.RequestUri, "cookie_ws");
            objWebRequest.CookieContainer = cookieC;
            //get response for request
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 7:26 AM

All replies

  • User-60428002 posted

    It looks like it's asynchronize, not untile you get the return after you request, it automatically redirect to another page, here, could you add some logic, only when you get the return, the code can go to next line.

    Tuesday, January 27, 2009 1:12 AM
  • User910780453 posted

    I was ignorant of Cookie/Sessions exchange between server and client during the requests and responses. So added this code and found it working - 

     CookieContainer cookieC = new CookieContainer();
    //objWebRequest.CookieContainer.SetCookies(objWebRequest.RequestUri, "cookie_ws");
    objWebRequest.CookieContainer = cookieC;
    //get response for request
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 7:26 AM
  • User910780453 posted

    I was ignorant of Cookie/Sessions exchange between server and client during the requests and responses. So added this code and found it working - 

     CookieContainer cookieC = new CookieContainer();
            //objWebRequest.CookieContainer.SetCookies(objWebRequest.RequestUri, "cookie_ws");
            objWebRequest.CookieContainer = cookieC;
            //get response for request
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 27, 2009 7:26 AM