Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
we have http post method in class mention below

C#
public static class Http
        {
            public static byte[] Post(string uri, NameValueCollection pairs)
            {
                byte[] response = null;
                using (WebClient client = new WebClient())
                {
                    response = client.UploadValues(uri, pairs);
                }
            return response;
            }
        }   

we are tyring to login but not able to login,
please see the post method and its parameter mention below.

C#
var response = Http.Post("https://ca-test.com/login.shtml", new     NameValueCollection() {
       { "j_account", "XXXX" },
       { "j_username", "XXXX" },
       { "j_password", "XXXX" },
   });
Posted
Updated 21-Apr-15 1:43am
v2
Comments
ZurdoDev 21-Apr-15 9:04am    
What's the error?
PrakashCs.net 23-Apr-15 0:29am    
Hi Sorry for late reply , actually is does not give any error it gives response as login page again, there is one more parameter formhash which generate when we first time hit url and it returns random form hash and that we need to send with post data i.e.
1. formhash, 2. account 3.username 4.password

1 solution

Hi,

are you sending credentials in http headers? if its true please follow below code.

public void PostMethod()
{
try
{
string svcCred = "credentials";

string textJSONdata = System.IO.File.ReadAllText(@"D:\data\jsontext.txt");

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, textJSONdata);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);

WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Headers.Add("Authorization", svcCred);
webClient.Encoding = Encoding.UTF8;
webClient.UploadString(@"http://testURL.com/Service.svc/UpdateGenformQuestions", "POST", data);

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}


}
 
Share this answer
 

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