c# multipart/form-data submit programmatically

前端 未结 4 1238
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 06:30

So got an small problem. Im creating an small application to automate an form submission on one website. But the bad thing is that they are using multipart/form-data for tha

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 07:01

    posts os multipart/form-data type have a different structure because they are meant to transfer data and not just plain text.

    Here's the format:

    --[random number, a GUID is good here]
    Content-Disposition: form-data; name="[name of variable]"
    
    [actual value]
    --[random number, a GUID is good here]--
    

    Using HTTPWebRequest you can create a request that has that format. Here's a sample:

    string boundary = Guid.NewGuid().ToString();
    string header = string.Format("--{0}", boundary);
    string footer = string.Format("--{0}--", boundary);
    
    StringBuilder contents = new StringBuilder();
    contents.AppendLine(header);
    
    contents.AppendLine(header);
    contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "username"));
    contents.AppendLine();
    contents.AppendLine("your_username");
    
    contents.AppendLine(header);
    contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "password"));
    contents.AppendLine();
    contents.AppendLine("your_password");
    
    contents.AppendLine(footer);
    

提交回复
热议问题