-1

I need to POST HTML Form programmatically containing some input parameters and a zip file with ContentType=application/zip.

<form id="form1" method="post" action="http://subdomain.domain.com/" runat="server"  enctype="multipart/form-data">
<div>
    <input type="file" id="file" name="file" value=""  />
    <input type="text" name="param1" value="valparam1" />
    <input type="text" name="param2" value="valparam2" />
    <input type="text" name="param3" value="valparam3" />
    <input id="Submit1" type="submit" value="Send"/>
</div>

In short, I want to simulate the above behaviour programmatically (POSTing the file ContentType as well).

I would prefer WebClient over HttpRequest;

alex
  • 2,464
  • 23
  • 32
Muhammad Rizwan
  • 348
  • 2
  • 12

1 Answers1

0

Not exactly but you need something like this (Sample from one of the project I worked upon), you can do the changes as per your requirements.

    string url = "Your url";
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "application/json";

    List<string> values = new List<string>();
    values.Add(groupValue);

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {

        string json = new JavaScriptSerializer().Serialize(new
        {
            apikey = apiKey,
            id = listId,
            name = groupName,
            type = "radio",
            groups = values

        });

        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();

        var response = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            var grouping = new JavaScriptSerializer().Deserialize(result, typeof(MailchimpGrouping));
            if (grouping != null)
            {
                return (grouping as MailchimpGrouping).Id;
            }
        }

    }
    return 0;

}
shrekDeep
  • 2,260
  • 7
  • 27
  • 39
  • Thanks for your answer sadeep. In my case, I want to upload file as well with a specific ContentType=application/zip. In your snippet, request is JSON but in my case it should be of form submission and the file ContentType should be"applicaiton/zip" when the server side checks with Request.Files[0].ContentType – Muhammad Rizwan Nov 17 '14 at 17:30