WebRequest From WinForm Application

private void btnSave_Click(object sender, EventArgs e)
{
    try
    {

        btnSave.Enabled = false;

        String filePath = txtVoiceFileName.Text;

        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("http://182.161.999.111:2022/Home/ReceiveAudio/");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        // string postData = "This is a test that posts this string to a Web server.";


        NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
        outgoingQueryString.Add("CustomerName", txtCustomerName.Text.Trim() );
        outgoingQueryString.Add("ContactNumber", txtMsisdn.Text.Trim());
           


        string postData = outgoingQueryString.ToString();

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        MessageBox.Show(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

        // String filePath = txtVoiceFileName.Text;
        var FileUploadResponse = WebHelper.MakeRequest("http://182.160.99.115:2022/Home/UploadAudio/",
                            new
                            {
                                wavFile = new FileInfo(filePath)
                                //Test = "Hello"
                                //File1 = new UploadFile(@"text\plain", "file1.txt", new MemoryStream(Encoding.UTF8.GetBytes("Hello World"))),
                                //File2 = new FileInfo("MyFile.txt")
                            });
        var FileUploadResult = FileUploadResponse.ResultToString();

    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message);                
    }
    finally { btnSave.Enabled = true; }
}



//===============================================

[HttpPost]
public JsonResult ReceiveAudio(FormCollection data)
{
    try
    {
        string CustomerName = data["CustomerName"].ToString();
        string ContactNumber = data["ContactNumber"].ToString();
        string IsNewSimActivation = data["IsNewSimActivation"].ToString();
        string IsSimReplacement = data["IsSimReplacement"].ToString();
        string IsRv = data["IsRv"].ToString();
        string IsDataActivation = data["IsDataActivation"].ToString();
        string IsDeviceOffers = data["IsDeviceOffers"].ToString();
        string IsVas = data["IsVas"].ToString();
        string IsCampMsg = data["IsCampMsg"].ToString();
        string CashReceived = data["CashReceived"].ToString();
        string IsEasyLoad = data["IsEasyLoad"].ToString();
        string VoiceFileName = data["VoiceFileName"].ToString();

        string ConnectionString = WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

           

        return Json("Success");
    }
    catch (Exception Ex)
    {
        return Json("Server says:" + Ex.Message, JsonRequestBehavior.AllowGet);

    }
}
Advertisements

Upload file from WinForm application to server

using BizArk.Core.Web; // http://bizark.codeplex.com/
var response = WebHelper.MakeRequest
                  ("http://Domain.com/Home/ReceiveAudio/",
                    new
                       {
                          wavFile = new FileInfo(e.FullPath)
                          //Test = "Hello",
                          //File1 = new UploadFile(@"text\plain", "file1.txt", 
                                   new MemoryStream(Encoding.UTF8.GetBytes("Hello "))),
                          //File2 = new FileInfo("MyFile.txt")
                        });

var result = response.ResultToString();