-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;

1
  • @Tarik, I viewed the link you gave, but in my case, I want to upload file as well with a specific ContentType. Commented Nov 17, 2014 at 12:05

1 Answer 1

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;

}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.