[英]Data retrieved from API using Postman different than when using C# program
Data which i retrieved from a sigfox api using postman is different than the data which i got when using c# program using http client. 我使用邮递员从sigfox api中检索到的数据与使用http客户端使用c#程序时获得的数据不同。
the api contains query parameters suchs as api包含查询参数,例如
limit=(a number) -- number which limits the number of messages i can receive(doesnt affect the problem) limit =(一个数字)-限制我可以接收的消息数量的数字(不影响问题)
and 和
before = (unix time) since= (unix time) which is the time parameters. before =(unix time)since =(unix time),这是时间参数。
In postman, the data i received follows the time parameter meaning data received is before set timing and since set timing. 在邮递员中,接收到的数据i遵循时间参数,这意味着接收到的数据是在设置定时之前并且从设置定时开始。
Api link(with fake info): https://backend.sigfox.com/api/devicetypes/devicetpes-id(filler)/messages?limit=100&before=1568186400&since=1568185800 api链接(带有虚假信息): https ://backend.sigfox.com/api/devicetypes/devicetpes-id(filler)/messages?limit =100& before=1568186400&since =1568185800
//this is for the download section of the data //这是用于数据的下载部分
public static class DownloadData
{
private static string UrlParameters="?limit=100&before=&since=";
public static void GetfromAPI(string deviceId, long beforeTime, long sinceTime)
{
var apiResponse = new Models.Datas();
var baseUrl = ConfigurationManager.AppSettings["ApiBaseUrl"];
var username = ConfigurationManager.AppSettings["ApiUserName"];
var password = ConfigurationManager.AppSettings["ApiPassword"];
var mediatype = ConfigurationManager.AppSettings["MediaType"];
var finalUrl = string.Format(baseUrl, deviceId, beforeTime, sinceTime);
using (var client = new ApiClient(finalUrl, username, password, mediatype))
{
//apiResponse = client.GetAsyncMessage<Models.Datas>(UrlParameters).Result;
apiResponse = client.GetAsyncMessage<Models.Datas>(UrlParameters).Result;
InsertToDatabase(FormatData(apiResponse.Data)); //apiresponse.data is holding the data
}
}
http client HTTP客户端
public class ApiClient : IDisposable
{
private HttpClient _httpClient;
private readonly string _baseurl;
private readonly string _username;
private readonly string _password;
private readonly string _mediatype;
public ApiClient(string baseurl, string username, string password , string mediatype)
{
_baseurl = baseurl;
_username = username;
_password = password;
_mediatype = mediatype;
}
public async Task<TResult> GetAsyncMessage<TResult>(string url) where TResult : class, new()
{
var strResponse = await GetAsyncMessage(url);
return JsonConvert.DeserializeObject<TResult>(strResponse, new JsonSerializerSettings ///str respons holding the string
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
}
private async Task<string> GetAsyncMessage(string url)
{
CheckHttpClientConnection();
using (var response = await _httpClient.GetAsync(url))
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
httpclientwith basic auth 具有基本身份验证的httpclient
private void CreateHttpClient()
{
_httpClient = new HttpClient { BaseAddress = new Uri(_baseurl) };
byte[] cred = UTF8Encoding.UTF8.GetBytes(_username + ":" + _password);
_httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
_httpClient.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(_mediatype));
}
a FUNCTION in another file which runs the top code 运行最高代码的另一个文件中的函数
DownloadData.GetfromAPI("devicetypesid filler",Common.BCurrenttimetoUnix(), Common.SCurrenttimetoUnix());
the common.bcurrentimetounix etc is taking the value from the function as the before and since query parameters.
So data i got in postman follows the query parameter. 因此,我在邮递员中获得的数据遵循查询参数。 meaning that data i got is in the before and since timing span. 表示我得到的数据是在时间间隔之前和之后。
example: 例:
before :4.50pm since 4.40pm is the timing set for params. 在:4.50pm之前,因为4.40pm是为参数设置的时间。
Data retrieved is 1st data = 4.49pm, 100th data= 4.41pm. 检索到的数据为第1个数据= 4.49pm,第100个数据= 4.41pm。
However in my c# program it does not even follow the parameter. 但是,在我的c#程序中,它甚至不遵循参数。 data retrieved exceed both since and before timing. 自定时以来和之前,检索到的数据均超过。
EDIT #1. 编辑#1。 Images link : https://imgur.com/a/rYc4Vhv ? 图片链接: https : //imgur.com/a/rYc4Vhv ?
EDIT #2 编辑#2
BASEURL : BASEURL:
<add key="ApiBaseUrl" value="https://api.sigfox.com/v2/device-types/{0}/messages" />
Edit #3 编辑#3
Working with this as baseurl: Link to debugging screenshots : https://imgur.com/a/47sFTao 使用它作为baseurl:链接到调试屏幕截图: https ://imgur.com/a/47sFTao
<add key="ApiBaseUrl" value="https://api.sigfox.com/v2/device-types/{0}/messages?limit=100&before={1}&since={2}" />
Edit #4 编辑#4
This code is working. 该代码有效。
public static void GetfromAPI(string deviceId, long beforeTime, long sinceTime)
{
var apiResponse = new Models.Datas();
var baseUrl = ConfigurationManager.AppSettings["ApiBaseUrl"];
var username = ConfigurationManager.AppSettings["ApiUserName"];
var password = ConfigurationManager.AppSettings["ApiPassword"];
var mediatype = ConfigurationManager.AppSettings["MediaType"];
var urlparam = ConfigurationManager.AppSettings["ApiParam"];
var finalurl = string.Format(baseUrl, deviceId);
var urlParam = $"?limit=100&before={beforeTime}&since={sinceTime}";
using (var client = new ApiClient(finalurl, username, password, mediatype))
{
//apiResponse = client.GetAsyncMessage<Models.Datas>(UrlParameters).Result;
apiResponse = client.GetAsyncMessage<Models.Datas>(urlParam).Result;
InsertToDatabase(FormatData(apiResponse.Data)); //apiresponse.data is holding the data
}
You are setting your UrlParameters
variable without values for before
or since
:- 您正在设置UrlParameters
变量,但不包含before
或since
值:
private static string UrlParameters="?limit=100&before=&since=";
And you are not updating that before you send it; 而且,您在发送之前不会进行更新; where you set finalUrl
, your string.Format
doesn't reference UrlParameters
; 在设置finalUrl
,您的string.Format
不引用UrlParameters
; hence you are gettings values outside your specified beforeTime
and afterTime
. 因此,您获取的值超出了指定的beforeTime
和afterTime
。
It's not clear what ApiClient you are using, however, here is an example using the basic WebClient
:- 目前尚不清楚您使用的是哪种ApiClient,但是,以下是使用基本WebClient
的示例:
var baseUrl = "https://backend.sigfox.com/api/devicetypes";
var deviceId = "12345";
var before = 1568186400;
var since = 1568185800;
var url = $@"{baseUrl}/{deviceId}/messages?limit=100&before={before}&since={since}";
var json = new System.Net.WebClient().DownloadString(url);
EDIT 编辑
Try updating your GetfromAPI
method like this (I've fixed how finalUrl
is constructed, and pass it, instead of UrlParameters
to your GetAsyncMessage
method and I pass baseUrl
instead of finalUrl
to your ApiClient
constructor):- 尝试更新GetfromAPI
方法是这样的(我已经固定如何finalUrl
构造,并通过它,而不是UrlParameters
您GetAsyncMessage
方法和我通过baseUrl
而不是finalUrl
您ApiClient
构造函数): -
public static class DownloadData
{
public static void GetfromAPI(string deviceId, long beforeTime, long sinceTime)
{
var apiResponse = new Models.Datas();
var baseUrl = ConfigurationManager.AppSettings["ApiBaseUrl"];
var username = ConfigurationManager.AppSettings["ApiUserName"];
var password = ConfigurationManager.AppSettings["ApiPassword"];
var mediatype = ConfigurationManager.AppSettings["MediaType"];
var finalUrl = $"api/devicetypes/{deviceId}/messages?limit=100&before={beforeTime}&since={sinceTime}";
using (var client = new ApiClient(baseUrl, username, password, mediatype))
{
apiResponse = client.GetAsyncMessage<Models.Datas>(finalUrl).Result;
InsertToDatabase(FormatData(apiResponse.Data)); //apiresponse.data is holding the data
}
}
}
NOTE: You may need to fiddle with finalUrl
depending on what your baseUrl
is set to (I can't see that here); 注意:您可能需要乱动finalUrl
取决于你baseUrl
设置为(我看不出这里); the idea is to check that url
value just before you pass it to HttpClient
so you can confirm what it's trying to fetch. 这个想法是在将url
值传递给HttpClient
之前检查该url
值,以便您可以确认其试图获取的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.