J.Proud
J.Proud

Reputation: 57

Error 404 Posting data from web client to web server

I cannot figure this out for the life of me, basically, I am using Visual Studio to implement a client-server system. I am new to this so this might seem like a simple problem.

I want to simply be able to allow the user to register to the system and store their details on SQL server. I have a web client which has a communication class to communicate with the web server, on this communication class I am trying to post the details that the user has input and route them to the necessary class on the server side which will deal with inserting the record.

I know the code is correct I am just pretty sure there is some syntax error or something. Basically I am getting a 404 error not found. I have checked for hours that my url address is exactly the same on both client and server side. Any help would be appreciated, this has been a nightmare for me.

Also the class 'UserM' is just the model that contains the information about the user table. I have also rechecked the ports and they are correct.

CLIENT SIDE COMMUNICATION CLASS:

private static string URL = "http://localhost:12804/api/Users/";

    public static UserM Register(UserM user)
    {

       var request = HttpWebRequest.Create(String.Format(URL + "Register?email={0}&password={1},forename={2},surname={3},dob={4},balance={5},nooflogins={6},joindate={7}", user.Email, user.Password, user.Forename, user.Surname, user.DoB, user.Balance, user.NoOfLogins, user.JoinDate));// "/save" or "/update"

        request.ContentType = "application/json"; // tell the API we want Json returned
        request.Method = "POST";

        try
        {
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string JSONPost = JsonConvert.SerializeObject(user, Formatting.None,
                    new JsonSerializerSettings()
                   {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                streamWriter.Write(JSONPost);
                streamWriter.Flush();
                streamWriter.Close();
            }


           using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return null;
                }
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return JsonConvert.DeserializeObject<UserM>(reader.ReadToEnd()); //if user is succesfully inserted into database
                }
           }
        }
        catch (Exception ex)
        {
            return null;
        }

    }

SERVER SIDE CLASS:

[Route("api/Users/Register"), AcceptVerbs("GET")]
     public UserM RegisterUser(string email, string password, string forename, string surname, DateTime DoB, decimal balance, int NoOfLogins, DateTime joindate) 
    { 
        using (TestDBDataContext tdb = new TestDBDataContext())
        {
            //check is user already exists in database
            if (tdb.User.Where(x => x.Email == email).Count() <= 0)
            {
                User temp = new User()
                {
                    Email = email,
                    Password = password,
                    Forename = forename,
                    Surname = surname,
                    DoB = DoB,
                    Balance = balance,
                    NoOfLogins = NoOfLogins,
                    JoinDate = joindate
                };

                tdb.User.InsertOnSubmit(temp);

                try
                {
                    tdb.SubmitChanges();
                    return new UserM(temp);
                }
                catch (Exception ex)
                {
                    return null;
                }
            } 
            else
            {
                return null;
            }

        }
    }

Upvotes: 1

Views: 507

Answers (1)

Ilya Chumakov
Ilya Chumakov

Reputation: 25029

Use the ampersand & instead of comma , in your query string:

&password={1}&forename={2}&surname={3}...

Upvotes: 1

Related Questions