如何在 C# 中使用 webRequest Class 接收和显示 LOB.com 明信片 cURL/API 请求
How to receive and show LOB.com postcard cURL/API requests in C# with webRequest Class
我正在尝试使用 CURL 从 LOB.com 帐户获取明信片信息。
lob.com 他们提供:
curl https://api.lob.com/v1/postcards/psc_5c002b86ce47537a \
-u test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:
如何使用此 CURL 获得 json 响应?
我用过
var httpWebRequest = (HttpWebRequest) WebRequest.Create("https://api.lob.com/v1/postcards/psc_95d1eafeb5b9a766");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "*/*";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("Authorization", "test_48d2bbf75c2edc75155c401d119bfae5526:");
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
using(var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
gta_allCustomersResponse answer = JsonConvert.DeserializeObject < gta_allCustomersResponse > (streamReader.ReadToEnd());
return answer;
如果我 运行 此代码,上面的代码显示 422(无法处理的实体)或 400(错误的请求)错误。
我建议使用 RestSharp 向 Lob 发送简单的 HTTP 请求。
或者您可以使用 HttpClient 实现:
public static void Main()
{
string API_KEY = "YourApiKey";
HttpClient httpClient = new HttpClient();
var authHeaderBytes = Encoding.ASCII.GetBytes(API_KEY + ":");
httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeaderBytes));
var requestPayload = new Dictionary<string, string>
{
{ "name", "John Doe" },
{ "address_line1", "123 Main St" },
{ "address_city", "San Francisco" },
{ "address_state", "CA" },
{ "address_zip", "94107" },
{ "address_country", "US" }
};
var encodedRequestForm = new FormUrlEncodedContent(requestPayload);
var APIResponse = httpClient.PostAsync("https://api.lob.com/v1/addresses", encodedRequestForm).GetAwaiter().GetResult();
var addressString = APIResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
System.Console.WriteLine(addressString);
}
此示例用于地址,您可以为明信片开发相同的示例,只需查看文档中的 curl 请求或 Java 请求并相应地编写代码。
希望对您有所帮助!
我正在尝试使用 CURL 从 LOB.com 帐户获取明信片信息。
lob.com 他们提供:
curl https://api.lob.com/v1/postcards/psc_5c002b86ce47537a \
-u test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc:
如何使用此 CURL 获得 json 响应?
我用过
var httpWebRequest = (HttpWebRequest) WebRequest.Create("https://api.lob.com/v1/postcards/psc_95d1eafeb5b9a766");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "*/*";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("Authorization", "test_48d2bbf75c2edc75155c401d119bfae5526:");
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
using(var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
gta_allCustomersResponse answer = JsonConvert.DeserializeObject < gta_allCustomersResponse > (streamReader.ReadToEnd());
return answer;
如果我 运行 此代码,上面的代码显示 422(无法处理的实体)或 400(错误的请求)错误。
我建议使用 RestSharp 向 Lob 发送简单的 HTTP 请求。
或者您可以使用 HttpClient 实现:
public static void Main()
{
string API_KEY = "YourApiKey";
HttpClient httpClient = new HttpClient();
var authHeaderBytes = Encoding.ASCII.GetBytes(API_KEY + ":");
httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeaderBytes));
var requestPayload = new Dictionary<string, string>
{
{ "name", "John Doe" },
{ "address_line1", "123 Main St" },
{ "address_city", "San Francisco" },
{ "address_state", "CA" },
{ "address_zip", "94107" },
{ "address_country", "US" }
};
var encodedRequestForm = new FormUrlEncodedContent(requestPayload);
var APIResponse = httpClient.PostAsync("https://api.lob.com/v1/addresses", encodedRequestForm).GetAwaiter().GetResult();
var addressString = APIResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
System.Console.WriteLine(addressString);
}
此示例用于地址,您可以为明信片开发相同的示例,只需查看文档中的 curl 请求或 Java 请求并相应地编写代码。
希望对您有所帮助!