是否有简单的方法在.NET中使用JSON来确保密钥以小写形式发送?
目前我正在使用newtonsoft的Json.NET库并且只是使用
string loginRequest = JsonConvert.SerializeObject(auth);
Run Code Online (Sandbox Code Playgroud)
在这种情况下auth只是以下对象
public class Authority
{
public string Username { get; set; }
public string ApiToken { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这导致了
{"Username":"Mark","ApiToken":"xyzABC1234"}
Run Code Online (Sandbox Code Playgroud)
有没有办法确保username和apitoken键以小写形式出现?
我不想简单地通过运行它String.ToLower(),当然,因为价值观username和apitoken是混合的情况.
我意识到我可以以编程方式执行此操作并手动创建JSON字符串,但我需要大约20个左右的JSON数据字符串,我看到我是否可以节省一些时间.我想知道是否有任何已经构建的库允许您强制使用小写来创建密钥.
鉴于以下尝试将数据发布到生成PDF文件的Web服务,PDF rocket(顺便说一句,这很棒).
我收到错误无效的URI:uri字符串太长
为什么有人会对POSTed数据施加任意限制?
using (var client = new HttpClient())
{
// Build the conversion options
var options = new Dictionary<string, string>
{
{ "value", html },
{ "apikey", ConfigurationManager.AppSettings["pdf:key"] },
{ "MarginLeft", "10" },
{ "MarginRight", "10" }
};
// THIS LINE RAISES THE EXCEPTION
var content = new FormUrlEncodedContent(options);
var response = await client.PostAsync("https://api.html2pdfrocket.com/pdf", content);
var result = await response.Content.ReadAsByteArrayAsync();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个荒谬的错误.
{System.UriFormatException: Invalid URI: The Uri string is …Run Code Online (Sandbox Code Playgroud) 我正在尝试将内容发布到我的服务器。这就是我过去一直这样做的方式,直到我不得不使用字符串以外的对象为止。
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(authType, tokens);
var postParams = new Dictionary<string, object>();
postParams.Add("string", string);
postParams.Add("int", string);
postParams.Add("datetime", DateTime);
postParams.Add("datetime", DateTime);
postParams.Add("Match", Match);
postParams.Add("TicketId", token);
using (var postContent = new FormUrlEncodedContent(postParams.ToDictionary()))
{
var myContent = JsonConvert.SerializeObject(postParams);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (HttpResponseMessage response = await client.PostAsync(@"http://url/api", byteContent))
{
response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
using (HttpContent content = response.Content)
{
string result = …Run Code Online (Sandbox Code Playgroud) 我试图为Post方法准备一个Json有效负载.服务器无法解析我的数据.我的值上的ToString()方法不能正确地将它转换为Json,请你建议正确的方法.
var values = new Dictionary<string, string>
{
{"type", "a"}, {"card", "2"}
};
var data = new StringContent(values.ToSttring(), Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var response = client.PostAsync(myUrl, data).Result;
using (HttpContent content = response.content)
{
result = response.content.ReadAsStringAsync().Result;
}
Run Code Online (Sandbox Code Playgroud)