1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Posting the Data Asp.net to Php website

Discussion in 'Programming' started by garry777, Feb 17, 2012.

  1. #1
    Hi,


    I am trying to posting the data asp.net to Php website but not successful.

    here is my code

    public void MyPostData()
    {
    string boundary = Guid.NewGuid().ToString();
    HttpWebRequest request = HttpWebRequest.Create("http://www.abc.com/register.php")
    as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
    request.PreAuthenticate = true;
    request.KeepAlive = true;
    StringBuilder sb = new StringBuilder();




    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterFirstName\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Gary");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterLastName\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("S");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterEmailAddress\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("abc@gmail.com");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterPassword\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("123456");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterPasswordConfirm\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("123456");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterHomeLocation\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("b27 7xu");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterReferer\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("10");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterJobAlerts\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("1");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterNewsletterOptions\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("1");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"CandidateRegisterAcceptTerms\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("1");
    sb.AppendFormat("\r\n");




    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"XiFormSubmit\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Submit");
    sb.AppendFormat("\r\n");


    sb.AppendFormat("--{0}--", boundary);
    byte[] fulldata = Encoding.Default.GetBytes(sb.ToString());
    request.ContentLength = fulldata.Length;
    using (Stream sw = request.GetRequestStream())
    {
    sw.Write(fulldata, 0, fulldata.Length);
    }
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
    HttpContext.Current.Response.Write(sr.ReadToEnd());
    }
    }


    Can any body help to sort my issue....

    Many Thanks
    Gary
     
    garry777, Feb 17, 2012 IP
  2. partn32

    partn32 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use query string variables for the passing the data
     
    partn32, Feb 18, 2012 IP
  3. garry777

    garry777 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My Problem has been resolved. Php Website using the cookies concept so i have added the cookie parameter and it works :) .

    CookieContainer cookies = new CookieContainer();
    HttpWebRequest request = HttpWebRequest.Create("http://www.abc.com/register.php")
    as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
    request.PreAuthenticate = true;
    // request.KeepAlive = true;
    request.CookieContainer = cookies;
     
    garry777, Feb 21, 2012 IP
  4. garry777

    garry777 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One more site i am trying to integrate the same functionality but facing the issue. Application is not accepting the values. Another website(PHP) using a the session concept. I am just trying to register the user profile which could be completed in two steps.

    Any body can sort out this issue. Thanks in Advanced.
     
    garry777, Feb 21, 2012 IP
  5. Muisings Freelance Contne

    Muisings Freelance Contne Member

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #5
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Get the API spec for the site you want to post to, then you'll see exactly what the site needs. You can't just guess these things.
     
    Rukbat, Feb 25, 2012 IP
  7. GinniSingh

    GinniSingh Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #7
    Hi Garry,

    Even, I have to do the same task but in different manner, As, We have one application in .NET and other is in PHP, In both applications, we create profiles. But, Now the requirement is when we create profile in our .NET application then same details will be inserted in our PHP application i.e. in one single click same data inserted in both .NET and php.

    But, before inserting any record we need to Log-in the site and then data will be inserted.

    I try to use your code but no success in it.

    Please let me where I am wrong.


    string boundary = Guid.NewGuid().ToString();
    CookieContainer cookies = new CookieContainer();
    HttpWebRequest request = HttpWebRequest.Create("http://64.50.177.73/") as HttpWebRequest;
    request.Method = "POST";
    request.CookieContainer = cookies;

    StringBuilder sb = new StringBuilder();

    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"login\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Admin");
    sb.AppendFormat("\r\n");

    sb.AppendFormat("--{0}", boundary);
    sb.AppendFormat("\r\n");
    sb.AppendFormat("Content-Disposition: form-data; name=\"pass\"");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("\r\n");
    sb.AppendFormat("P@ssword");
    sb.AppendFormat("\r\n");

    sb.AppendFormat("--{0}", boundary);
    byte[] fulldata = Encoding.Default.GetBytes(sb.ToString());
    request.ContentLength = fulldata.Length;
    using (Stream sw = request.GetRequestStream())
    {
    sw.Write(fulldata, 0, fulldata.Length);
    }

    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
    HttpContext.Current.Response.Write(sr.ReadToEnd());
    }


    Thanks.
     
    GinniSingh, Apr 15, 2013 IP
  8. GinniSingh

    GinniSingh Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8

    Hi Rukbat,

    I have an PHP api with me, how can access it (all it's methods) in my asp.net code?

    Thanks.
     
    GinniSingh, Apr 15, 2013 IP