dimanche 12 juin 2016

The remote server returned an error: (500) Internal Server Error. Web API

http://ift.tt/1UuRsua

below code i use to post method from a log.in page but give me an error.. thanks in advance. anyone have an idea or similar to my problem? please share

public ActionResult SaveNameAPI(string username, string password) {

        bool isValid = false;
        HttpWebRequest hwrequest;
        HttpWebResponse hwresponse;
        string POSTdata = null;
        string responseData = "";
        HttpClient client = new HttpClient();
        HttpWebResponse response;
        Stream postStream;

        string url = "(URL).asmx?op=ValidateLogin?username=" + username + "&password=" + password;

        hwrequest = (HttpWebRequest)WebRequest.Create(url);
        hwrequest.Accept = "*/*";
        hwrequest.AllowAutoRedirect = true;
        hwrequest.UserAgent = "http_requester/0.1";
        hwrequest.Timeout = 60000;
        hwrequest.Method = "POST";

        byte[] byteData;
        if (hwrequest.Method == "POST")
        {
            hwrequest.ContentType = "text/xml";
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();              

            POSTdata = "username=" + username + "&password=" + password;
            byteData = encoding.GetBytes(POSTdata); 
            hwrequest.ContentLength = byteData.Length;
            postStream = hwrequest.GetRequestStream();
            postStream.Write(byteData, 0, byteData.Length);
            postStream.Close();
            response = (HttpWebResponse)hwrequest.GetResponse(); <<-- this line gives me an error
        }          
        hwresponse = (HttpWebResponse)hwrequest.GetResponse();
        if (hwresponse.StatusCode == System.Net.HttpStatusCode.OK)
        {
            StreamReader responseStream = new StreamReader(hwresponse.GetResponseStream());
            responseData = responseStream.ReadToEnd();
        }
        hwresponse.Close();
        var result = new JsonResult();
        result.Data = new
        {
            username= username,
            password= password

        };
        return result;

    }

Aucun commentaire:

Enregistrer un commentaire