我正在尝试使用restsharp来使用休息服务(wcf)
这是我的服务
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "/PEmploy", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
Employee PostGetEmploy(Employee emp);
}
[DataContract]
public class Employee
{
[DataMember]
public int EmpNo { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public string DeptName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我就是这么称呼它的
var client = new RestClient("http://localhost:14437/Service.svc");
var request = new RestRequest("XmlService/PEmploy", Method.POST);
myRef.Employee emp = new myRef.Employee() { EmpNo = 101, EmpName = "Mahesh", DeptName = "CTD" }; …Run Code Online (Sandbox Code Playgroud) 我正在尝试将用户名和密码传递到以下 URL:
https://maxcvservices.dnb.com/rest/Authentication
Run Code Online (Sandbox Code Playgroud)
根据文档,user_id 和密码必须作为标头传递,并分别带有键:x-dnb-user、x-dnb-pwd。
到目前为止,我有以下代码似乎可以工作,但我无法检索响应对象返回的身份验证令牌:
public static void Main (string[] args)
{
var client = new RestClient ("https://maxcvservices.dnb.com/rest/Authentication");
var request = new RestRequest (Method.POST);
request.AddHeader("x-dnb-user", myEmail);
request.AddHeader("x-dnb-pwd", myPassword);
IRestResponse resp = client.Execute(request);
var content = resp.Content;
Console.WriteLine (resp.StatusDescription);
Console.WriteLine (resp.StatusCode);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试打印内容时,我得到一个空行,但我实际上期望的是服务返回的身份验证令牌。我认为我在代码中做的几件事(但不确定)是将用户 ID 和密码作为 POST 请求中的标头传递,这是所需的。该令牌作为响应对象中“授权”字段的值返回。我想知道如何打印令牌。另外,statusDescription、statusCode 都打印 OK,这告诉我我有正确的请求,但无法在响应中找到身份验证令牌。如果能指导我如何访问返回的 POST 响应的授权字段中的身份验证令牌,我们将不胜感激。
我正在尝试发送POST请求并以特定格式格式化查询字符串。除了第一个参数之外,顺序并不重要,但我还没有成功。
我需要的:
本地主机/someapp/api/dosomething/
5335?save=false&userid=66462
我的一些尝试吐出了什么:
http://localhost/someapp/api/dosomething/?Id=29455&save=false&userId=797979 http://localhost/someapp/api/dosomething/?save=false&userId=797979
我如何格式化请求:
request.AddQueryParameter("Id", "29455");
request.AddQueryParameter("save", "false");
request.AddQueryParameter("user", "4563533245");
Run Code Online (Sandbox Code Playgroud)
如果我尝试AddParameter它Id不会附加到查询字符串上(我想因为它是 POST 而不是 GET),所以这是行不通的。API 不需要表单,它需要:
(string id, List<Dictionary<string,string>>)
我可以使用 a StringBuilder,但这感觉不对。我不确定这是否UrlSegment是最好的方法,因为我基本上会破解查询字符串。有没有办法使用 RestSharp 的 API 将我的请求格式化为我需要的格式?
我尝试使用 Postman 将不记名令牌发送到 Auth0 API,效果非常好。
然后我尝试使用 RestSharp (在 c# 中)进行相同的操作,但它根本不起作用。
下面是我的代码。我尝试了许多不同的格式,但没有一个能工作。我还有其他方法可以尝试使其工作吗?
var client = new RestClient("http://domain.auth0.com/api/v2/users");
RestRequest request = new RestRequest(Method.GET);
//request.AddHeader("authorization", "Bearer eyJhbGcJ9.eyJhdWQiOiJ6VU4hVWUE2.token");
//request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//request.AddHeader("Accept", "application/json");
//RestClient client = new RestClient("http://domain.auth0.com");
//RestRequest request = new RestRequest("api/v2/users", Method.GET);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");
request.AddParameter("Authorization",
string.Format("Bearer " + "eyJhbGciOI1NiIsI9.eyJhdWQiOiWmVhTWpD2VycyI6eyJhY.token"),
ParameterType.HttpHeader);
//request.AddParameter("Authorization",
// String.Format("Bearer {0}", token),
//ParameterType.HttpHeader);
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
PS:令牌已更改。
我正在使用的 Rest API 有一个名为 Api-Key 的新字段。这不是有效的 C# 字段名称,因此我想知道是否有不同的方式来构建主体。
var client = new RestClient("https://TestWeb");
var request = new RestRequest("login", Method.POST);
request.AddHeader("Content-type", "application/json");
request.AddJsonBody(
new {
credentials =
new
{
username = "Uname",
password = "password",
Api-Key = "apikey"
} });
Run Code Online (Sandbox Code Playgroud) 我对于自动化测试来说是一个相对菜鸟 - 目前,我正在使用 Postman 为 Visual Studio 2017 Enterprise 中的 API 请求生成 RestSharp 代码。
本质上,我正在创建一个基本的单元测试,然后放入代码来执行测试
我需要知道的是 - 在我的测试中,我是否可以首先进行登录调用并保存我的不记名令牌,以便在所有后续调用中使用 - 在邮递员中,这很容易,因为我可以设置一个环境将其拉入的变量。
所以本质上,我需要做的是进行登录调用以返回不记名令牌,保存不记名令牌,然后在我进行的后续调用中使用该令牌
任何帮助是极大的赞赏!
我正在尝试使用restClient (RestSharp) 请求从 c# 类上传文件。我正在创建 Method.POST 方法并将一个音频文件添加到此请求中作为 multipart/form-data。
当我执行请求时服务器抛出异常。
例外 :
System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at Groove.Libraries.Helper.EnumHelper.ParseEnum[T](String value) in D:\project\Groove\Web\Groove\Libraries\Helper\EnumHelper.cs:line 47
at Groove.Controllers.Api.DocumentController.
<PostDocumentUpload>d__5.MoveNext()
Run Code Online (Sandbox Code Playgroud)
RestClient 请求代码:
string api_url = "http://localhost:57997/";
var fullFileName = "Adios.mp3";
var filepath = @"C:\Users\Admin\Desktop\Adios.mp3";
RestClient client = new RestClient(ApiModel.api_url);
var request = new RestRequest("api/document", Method.POST);
request.AddFile(Path.GetFileNameWithoutExtension(fullFileName), filepath);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("ReferenceType",28,ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
服务器代码: …
我正在使用 RestSharp 进行 GET api 调用。api 调用通过传递授权标头通过 HTTP 基本身份验证进行身份验证。
服务器使用状态代码 307 重定向 api 调用。我的客户端代码确实处理重定向,但授权标头未传递到此重定向的 api 调用。这样做是出于此处提到的正当理由。因此我确实收到了未经授权的错误。
如何配置 RestClient 来恢复授权标头?
var client = new RestClient("https://serverurl.com");
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic Z3JvdXAxOlByb2otMzI1");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Tenant-Id", "4892");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Run Code Online (Sandbox Code Playgroud) 我的休息要求:
RestSharp.RestClient uplClient = new RestSharp.RestClient();
RestSharp.RestRequest request = new RestSharp.RestRequest(IMAGE_UPLOAD_URI, Method.POST);
request.AddParameter("user", USER_HASH);
request.AddParameter("apikey", API_KEY);
request.AddFile("Filedata", file, "test.jpg","image/jpeg");
uplClient.ExecuteAsync(request, (response) =>
{
callback(response.Content, null);
if (response.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("Upload completed succesfully...\n" + response.Content);
}
else
{
MessageBox.Show(response.StatusCode + "\n" + response.StatusDescription);
}
});
Run Code Online (Sandbox Code Playgroud)
通过Fiddler检查响应时,原始数据是:
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Wed, 05 Dec 2012 11:35:57 GMT
Content-Type: text/javascript;charset=utf-8
Connection: keep-alive
Location: file:///Applications/Install/8014A556-A76A-4294-B375-6E3668177CCA/Install/?errorNr=0&picUploadId=1007501
Content-Length: 423
{"ok":true,"error":false,"imageIcon":0,"uid":1287837,"tmpId":1007501,"url":"http:\/\/i1.ifrype.com\/tmp\/10\/1007501.jpg","urlIcon":"http:\/\/i1.ifrype.com\/tmp\/10\/i_1007501.jpg","urlSmall":"http:\/\/i1.ifrype.com\/tmp\/10\/sm_1007501.jpg","urlMiddle":"http:\/\/i1.ifrype.com\/tmp\/10\/nm_1007501.jpg","urlLarge":"http:\/\/i1.ifrype.com\/tmp\/10\/l_1007501.jpg","urlGM":"http:\/\/i1.ifrype.com\/tmp\/10\/ngm_1007501.jpg"}
Run Code Online (Sandbox Code Playgroud)
虽然RestSharp将所有respose值显示为null.我认为这与默认的JSonDeserializer有关,它无法解析某些数据.下面是成功解析响应的原始数据:
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Dec 2012 11:28:13 …Run Code Online (Sandbox Code Playgroud) 我正在使用RestSharp调用Web服务并正确地恢复我的响应,但我得到的数据是一种奇怪的格式.它是一个GUIDS列表,这也是我想要的,但它们作为一个bool对象回来,看看:
"{
\"5916DF70-C413-4132-90F7-C365B0FAA26D\" : true,
\"B5F0FF80-F8D1-40F7-8313-045F02D37FAA\" : true,
\"D859A904-EDAE-4D87-9ADC-8FB5F3B47B02\" : true
}"
Run Code Online (Sandbox Code Playgroud)
我将如何解析,所以我只得到一个只包含GUID的List?