[英]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.