我想使用Mono + Restsharp在Raspberry上执行HttpPost.
我尝试在代码中重现的Httpie调用看起来像这样:
http POST https://XXXXX.azurewebsites.net/api/report key=value
Run Code Online (Sandbox Code Playgroud)
这是C#代码:
RestClient nodeRed = new RestClient("http://XXXXX.azurewebsites.net/");
var request = new RestRequest("api/report", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new { Timestamp = DateTime.Now, Data = "Test" });
request.Timeout = 5000;
var response = nodeRed.Execute(request);
Run Code Online (Sandbox Code Playgroud)
代码运行正常,它只是无法正常工作.响应不包含任何内容(在httpie/MS .NET Framework上具有"成功"的服务器响应).
我已经在Mono上发现了证书的内容.运行
sudo certmgr -ssl -v -m "https://XXXXX.azurewebsites.net"
Run Code Online (Sandbox Code Playgroud)
告诉我它第一次添加两个证书.再次运行它会再次添加第一个证书(并再次),因为它不起作用.
X.509证书v3发布自:C = IE,O =巴尔的摩,OU = CyberTrust,CN = Baltimore CyberTrust Root颁发给:C = US,S =华盛顿,L = Redmond,O = Microsoft Corporation,OU = Microsoft IT, CN = Microsoft IT SSL SHA2有效期:19/12/2013 20:07:32有效期至:19/12/2017 …
我正在使用C#.NET 3.5构建RESTful API客户端.
我第一次开始用好的HttpWebClient(和HttpWebResponse)来构建它,我可以做任何我想做的事.我很高兴.我唯一坚持的是JSON响应的自动反序列化.
所以,我听说过一个名为RestSharp(104.1)的精彩库,它可以简化RESTful API客户端的开发,并自动反序列化JSON和XML响应.我打开它在我的所有代码,但现在我知道我不能做的事情,我可以做HttpWebClient和HttpWebResponse,就像访问和编辑原始请求主体.
有人有解决方案吗?
编辑:我知道如何设置请求体(带request.AddBody()),我的问题是我想获取此请求体字符串,编辑它,并在请求中重新设置它(动态更新请求体)
我试图用RestSharp发布以下JSON:
{"UserName":"UAT1206252627",
"SecurityQuestion":{
"Id":"Q03",
"Answer":"Business",
"Hint":"The answer is Business"
},
}
Run Code Online (Sandbox Code Playgroud)
我认为我很接近,但我似乎正在努力使用SecurityQuestion(API正在抛出一个错误,说参数丢失,但它没有说哪一个)
这是我到目前为止的代码:
var request = new RestRequest("api/register", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("UserName", "UAT1206252627");
SecurityQuestion securityQuestion = new SecurityQuestion("Q03");
request.AddParameter("SecurityQuestion", request.JsonSerializer.Serialize(securityQuestion));
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
我的安全问题类看起来像这样:
public class SecurityQuestion
{
public string id {get; set;}
public string answer {get; set;}
public string hint {get; set;}
public SecurityQuestion(string id)
{
this.id = id;
answer = "Business";
hint = "The answer is Business";
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?是否有其他方式发布安全问题对象?
非常感谢.
有没有办法获取RestSharp请求的完整URL,包括其资源和查询字符串参数?
这个请求的IE:
RestClient client = new RestClient("http://www.some_domain.com");
RestRequest request = new RestRequest("some/resource", Method.GET);
request.AddParameter("some_param_name", "some_param_value", ParameterType.QueryString);
IRestResponse<ResponseData> response = client.Execute<ResponseData>(request);
Run Code Online (Sandbox Code Playgroud)
我想获得完整的请求网址:
http://www.some_domain.com/some/resource?some_param_name=some_param_value
Run Code Online (Sandbox Code Playgroud) 客户端接收正式的JSON内容"{\"Id\":[1,2,3],\"Size\":56}",但在反序列化字节数组时出错.
1以下语句中出现错误
IRestResponse<key> response = client.Execute<key>(request);
Run Code Online (Sandbox Code Playgroud)
2错误消息是"没有为此对象定义无参数构造函数".
3客户端大小的对象类与服务器端的对象类相同:
public class key
{
public byte[] id { get; set; }
public int Size { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
4我尝试通过JSON格式传递包含字符串和整数的对象,这一切都很好但是字节数组.
我有一个JSON响应,我正在尝试使用RestSharp反序列化,它看起来像这样:
{"devices":[{"device":{"id":7,"deviceid":"abc123","name":"Name"}},
{"device":{"id":1,"deviceid":"def456","name":"Name"}}],
"total":2,
"start":0,
"count":2}
Run Code Online (Sandbox Code Playgroud)
基于我发现的一些建议,我试图像这样设置我的POCO:
public class DevicesList
{
public List<DeviceContainer> Devices;
}
public class DeviceContainer
{
public Device Device;
}
public class Device
{
public int Id { get; set; }
public string DeviceId { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我的执行看起来像这样:
// execute the request
var response = client.Execute<DevicesList>(request);
Run Code Online (Sandbox Code Playgroud)
但是,response.Data是NULL,我尝试了其他变种没有运气.
那么,这种情况应该使用什么类结构和映射?我没有额外的DeviceContainer课程也试过这个.
谢谢您的帮助.
我试图通过基于身份验证的HTTPS调用本地托管的WCF REST服务.
这是有效的,授权标题通过很好,所有人都很高兴:
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
var request = (HttpWebRequest)WebRequest.Create("https://localhost/MyService/MyService.svc/");
request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add(
System.Net.HttpRequestHeader.Authorization,
"Basic " + this.EncodeBasicAuthenticationCredentials("UserA", "123"));
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream())
{
if (webStream != null)
{
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用RestSharp时,Authorization标头永远不会通过请求:
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
var credentials = this.EncodeBasicAuthenticationCredentials("UserA", "123");
var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/");
var restRq = new RestSharp.RestRequest("/");
restRq.Method = Method.GET;
restRq.RootElement = "/";
restRq.AddHeader("Authorization", "Basic " …Run Code Online (Sandbox Code Playgroud) 我在使用RestSharp时遇到问题我需要将REST API用于我正在进行的项目.我需要发出的请求分为三部分:头API密钥,要上传的文件和一堆JSON格式的数据.API要求使用表单字段名称"data"发送数据部分.由于某种原因,这会导致问题,因为它在请求正文中命名字段"data".
我的代码如下:
var request = new RestRequest(UPLOAD_DOC_URLSEGMENT, Method.POST)
{
RequestFormat = DataFormat.Json,
AlwaysMultipartFormData = true,
JsonSerializer = new RestSharpJsonDotNetSerializer(customSerializer)
};
if (doc is DocA)
request.AddParameter("data",doc as DocA,ParameterType.RequestBody);
//request.AddBody(doc as DocA);
else
request.AddParameter("data", doc as DocB,ParameterType.RequestBody);
//request.AddBody(doc as DocB);
request.AddFile("file", doc.File.FullName);
Run Code Online (Sandbox Code Playgroud)
如您所见,我试图同时使用request.AddBody(doc)方法和request.AddParameter(name, object, type)方法.它们似乎都没有正确发送数据,因为我收到服务器的响应,说明缺少必需的参数.使用fiddler,我可以看到二进制数据,但从不使用这两种方法的JSON数据.我已经浏览了RestSharp文档,但我找不到任何允许我将特定"字段"名称指定为表单数据体的"数据"的内容,这是我认为导致我遇到的问题.我在这做错了什么?
编辑:进一步检查fiddler后,它似乎没有将我的JSON数据添加到HTTP请求的主体.但是,在上传(执行命令)之前有一个断点,我可以在参数列表(和文件列表)中看到所有序列化的内容.当用Fiddler检查时,我看到文件二进制数据,然后是多部分/表格数据边界,然后什么都没有.我认为这是我的数据应该是......
我正在使用RestSharp来调用外部API.
这有效:
var client = new RestClient(apiUrl);
var request = new RestRequest(myurl, Method.GET);
foreach (var param in parameters)
{
request.AddQueryParameter(param.Key, param.Value);
}
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
这不是:
var client = new RestClient(apiUrl);
var request = new RestRequest(myurl, Method.GET);
foreach (var param in parameters)
{
request.AddParameter(param.Key, param.Value);
}
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
导致:
System.Exception:API调用MyWebAPIMethod GET:状态码为0失败 - 无法连接到远程服务器
AddParameter和之间有什么区别AddQueryParameter?
根据文档,它们在使用HttpGET时应该起到相同的作用,根据Fiddler,它们似乎也生成相同的URL.
我已经浏览了http://restsharp.org/代码,这些代码很有用.下面是带有asp.net核心的RestSharp的代码.
public GenericResponseObject<T> GetGeneric<T>(string operation, params KeyValuePair<string, string>[] nameValues) where T : class
{
RestClient client = new RestClient(_baseUrl)
{
Authenticator = new HttpBasicAuthenticator(_username, _password)
};
RestRequest request = new RestRequest(operation, Method.GET);
foreach (KeyValuePair<string, string> nameValue in nameValues)
{
request.AddQueryParameter(nameValue.Key, nameValue.Value);
}
IRestResponse<GenericResponseObject<T>> response = client.Execute<GenericResponseObject<T>>(request);
GenericResponseObject<T> responseObject = response.Data;
return responseObject;
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码对我很有用.现在我想在asp.net核心中实现相同的代码.
我可以获得一个示例如何在asp.net核心中使用RestSharp.我添加了依赖项RestSharp.NetCore":105.2.3.