Post_Method and Get_Method from C#

  public static string Post_Method(string uri, string command)
    {
        WebRequest req = null;
        WebResponse rsp = null;
        string tempReply = "";
        try
        {
            req = WebRequest.Create(uri);
            req.Method = "post";
            req.ContentType = "application/x-www-form-urlencoded";
            StreamWriter writer = new StreamWriter(req.GetRequestStream());
            writer.Write(command);
            writer.Close();

            // Send the data to the webserver
            rsp = req.GetResponse();

            StreamReader reader = new StreamReader(rsp.GetResponseStream());
            tempReply = reader.ReadToEnd().ToString();
            tempReply = tempReply.Trim();
        }
        catch (Exception ex)
        {
            tempReply = "01";
        }
        return tempReply;
    }

    public static string Get_Method(string url)
    {
     
        string tempReply = "";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                tempReply = reader.ReadToEnd();
            }

            return tempReply;
        }
        catch (Exception ex)
        {
            tempReply = "01";
        }
        return tempReply;
    }

Comments

Popular posts from this blog

Multipart/form-data using basic auth

How to Capture image of HTML5 camera in asp.net

Simple API that receive MultipartData File with parameter in ASP.net Web API