简体   繁体   English

从文本为 RestSharp 请求生成多部分表单数据体

[英]Generating Multipart form data body from text for RestSharp request

I'm trying to get a basic login feature working using RestSharp.我正在尝试使用 RestSharp 获得基本的登录功能。 I have the login user and password as text that I need to convert somehow since the server accepts requests as multipart/form-data and my initial attempt was this:我有登录用户和密码作为我需要以某种方式转换的文本,因为服务器接受请求作为多部分/表单数据,我最初的尝试是这样的:

   RestRequest request = new RestRequest(resource, Method.POST);

   request.AddParameter("login", config.Login, ParameterType.RequestBody);
   request.AddParameter("pass", config.Password, ParameterType.RequestBody);

Only to find out I cannot add two params for request body, where it discards all but the first.只是发现我不能为请求正文添加两个参数,它会丢弃除第一个以外的所有参数。 So my next attempt was to build the requestBody:所以我的下一个尝试是构建 requestBody:

        MultipartFormDataContent formData = new MultipartFormDataContent();
        formData.Add(new StringContent(config.Login), "login");
        formData.Add(new StringContent(config.Password), "pass");
        request.AddBody(formData);

This also didnt work and just returned the closed element </MultipartFormDataContent>这也不起作用,只是返回了关闭的元素 </MultipartFormDataContent>

Last I tried to just pass them as post params rather than the body, which it seems to have just ignored considering it expects a form I guess:最后,我尝试将它们作为 post 参数而不是 body 传递,考虑到它需要一种我猜想的形式,它似乎只是忽略了它:

        request.AddParameter("login", JsonConvert.SerializeObject(config.Login), ParameterType.GetOrPost);
        request.AddParameter("pass", JsonConvert.SerializeObject(config.Password), ParameterType.GetOrPost);

I'm looking for any tips on how I can somehow convert and send this text as valid form-data.我正在寻找有关如何以某种方式将此文本转换为有效表单数据并将其发送的任何提示。 It looks easier to do with HTTPWebRequest rather than RestSharp but it's bothering me that I'm sure it's possible.使用 HTTPWebRequest 而不是 RestSharp 看起来更容易,但我确信这是可能的,这让我很困扰。

Thanks so much!非常感谢!

It turns out that the default headers generated by RestSharp were sending the boundary parameter with quotes, where my endpoint was receiving them without quotes, so I needed a lower level interface and ended up being able to create a custom header for boundary with HttpWebRequest and MediaHeadervalue that fit my needs.事实证明,RestSharp 生成的默认标头正在发送带引号的边界参数,而我的端点接收它们时不带引号,因此我需要一个较低级别的接口并最终能够创建一个自定义 header 用于与 HttpWebRequest 和 MediaHeadervalue 的边界适合我的需要。 Thanks everyone for looking!感谢大家观看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Restsharp 如何在 MultiPart/form-data 请求中为参数指定 ContentType - Restsharp how to specify ContentType for a parameter in a MultiPart/form-data Request 使用restsharp格式化多部分HTTP请求正文输出 - Formatting of multipart HTTP request body output with restsharp 如何为对象数组形成 RestSharp 请求正文 - How to form RestSharp Request body for array of objects 如何在Restsharp中使用multipart / form-data上传XML文件? - How to upload an XML file using multipart/form-data with Restsharp? 使用RestSharp在multipart / form-data POST中包含文件的问题 - Trouble including a file in multipart/form-data POST using RestSharp 如何在restsharp中发送multipart / form-data中的2个参数? - How can i send 2 parameters in multipart/form-data at restsharp? 使用RestSharp发送HTTP POST Multipart / form-data字段 - Sending HTTP POST Multipart/form-data field using RestSharp RestSharp 发布请求 - 具有 x-www-form-urlencoded 值的正文 - RestSharp post request - Body with x-www-form-urlencoded values 如何使用Content-Type:multipart / form-data通过Web API请求主体参数访问? - How request body parameter access by Web API using Content-Type: multipart/form-data? RestSharp 将正文作为文本/纯文本发送 - RestSharp send body as text/plain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM