C# 将http GET请求中的JSON数据传递给REST服务
使用以下命令:C# 将http GET请求中的JSON数据传递给REST服务,c#,rest,django-rest-framework,httprequest,C#,Rest,Django Rest Framework,Httprequest,使用以下命令: curl -v -X GET -H "Content-Type: application/json" -d {'"mode":"0"'} http://host.domain.abc.com:23423/api/start-trial-api/ 我能够将JSON数据发送到web请求并获得响应。 我怎样才能在C#中做到这一点 我能够将数据发布到其他服务并获得响应,但不知道如何发送数据以获取请求。 我尝试在google和stackoverflow上搜索C#中的相同内容,但没有找到任
curl -v -X GET -H "Content-Type: application/json" -d {'"mode":"0"'} http://host.domain.abc.com:23423/api/start-trial-api/
我能够将JSON数据发送到web请求并获得响应。
我怎样才能在C#中做到这一点
我能够将数据发布到其他服务并获得响应,但不知道如何发送数据以获取请求。
我尝试在google和stackoverflow上搜索C#中的相同内容,但没有找到任何内容。示例代码-确保请求方法设置为“GET”
这里有大量的
摘要
,但希望能提供一个关于如何连接到c35;
界面
public interface IShopifyAPIGateway
{
HttpResponseMessage Get(string path);
}
Shopify API网关,它稳定
HTTPClient()
用法返回类型必须与您的传球类型以及响应返回的类型相匹配
private IEnumerable<Order> Orders()
{
var entityRepo = new EntityRepository<Order>();
return entityRepo.Find("somedomain/api/orders?mode=0", _identity);
}
private IEnumerable Orders()
{
var entityRepo=新的EntityRepository();
返回entityRepo.Find(“somedomain/api/orders?mode=0”,_identity);
}
为什么这个问题被否决?请留下评论以便改进。如果它确实是一个REST服务,您会传递一个参数或构建到uri中吗。somedomain/api/0嘿,国王,你找到解决方案了吗?你能把它贴在这里吗?我也遇到了同样的问题。此代码没有需要传递的数据。我了解如何连接到web服务。我还能够对该服务进行POST呼叫,并成功获得响应。我只是想知道如何在GET请求中发送JSON数据。抱歉,我可能把Q解释错了。。。你说“其他服务”。这是您构建的服务吗?是web api吗。请更新有关.net环境的更多信息。当你说向上传递数据时,你指的是一个Id?一般来说(如您所知),GET请求通常是通过Id获取的。
public sealed class ShopifyAPIGateway : IShopifyAPIGateway
{
/// <summary>
///
/// </summary>
private Identity _identity;
/// <summary>
///
/// </summary>
private HttpClient _httpClient;
/// <summary>
///
/// </summary>
public ShopifyAPIGateway(Identity
identity)
{
_identity = identity;
_httpClient = new HttpClient(ClientHandler());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public HttpResponseMessage Get(string path)
{
try
{
var response = Connect().GetAsync(path).Result;
return response;
}
catch (CustomHttpResponseException ex)
{
new Email().SendEmail(_identity.ClientName, "Http Response Error - Shopify API Module",
"Http Response Error - Shopify API Module: " + ex.Message,
"error@retain.me");
throw new CustomHttpResponseException(ex.Message);
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private HttpClient Connect()
{
try
{
_httpClient.BaseAddress = new Uri(_identity.APIURL);
return _httpClient;
}
catch (CustomHttpRequestException ex)
{
throw new CustomHttpRequestException(ex.Message);
}
}
/// <summary>
///
/// </summary>
/// <param name="userKey"></param>
/// <returns></returns>
private HttpClientHandler ClientHandler()
{
try
{
return new HttpClientHandler()
{
Credentials = new NetworkCredential(_identity.APIKey,
_identity.Password),
PreAuthenticate = true
};
}
catch (CustomClientHandlerException ex)
{
throw new CustomClientHandlerException(ex.Message);
}
}
}
public sealed class EntityRepository<T> : IEntityRepository<T>
{
private IShopifyAPIGateway _gateWay;
public T Find(string path)
{
try
{
_gateWay = new ShopifyAPIGateway(_identity);
var json = _gateWay.Get(path).Content.ReadAsStringAsync();
T results = JsonConvert.DeserializeObject<T>(json.Result);
return results;
}
catch (System.Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
private IEnumerable<Order> Orders()
{
var entityRepo = new EntityRepository<Order>();
return entityRepo.Find("somedomain/api/orders?mode=0", _identity);
}