I am trying to connect my .NET Core service with GridDB using a REST API, but I am facing an issue while retrieving data.
Here's what I have done so far:
- Set Up GridDB Server.
- Installed and configured GridDB on my local machine.
- Verified that GridDB is running and accessible.
- Created a REST API Using Java (Spring Boot)
I built a simple REST API to expose GridDB data.
The API retrieves data from a specified container based on a given key.
Here's my API endpoint:
@RestController
@RequestMapping("/griddb")
public class GridDBController
{
private static final String GRIDDB_CLUSTER_NAME = "myCluster";
private static final String GRIDDB_NOTIFICATION_MEMBER = "***.0.0.1";
private static final String GRIDDB_USERNAME = "***";
private static final String GRIDDB_PASSWORD = "****";
@GetMapping("/get/{container}/{key}")
public ResponseEntity<String> getData(@PathVariable String container, @PathVariable String key)
{
try
{
GridStore store = GridStoreFactory.getInstance().getGridStore(
new Properties() {{
setProperty("notificationMember", GRIDDB_NOTIFICATION_MEMBER);
setProperty("clusterName", GRIDDB_CLUSTER_NAME);
setProperty("user", GRIDDB_USERNAME);
setProperty("password", GRIDDB_PASSWORD);
}});
Container<String, Row> col = store.getContainer(container);
Row row = col.get(key);
return ResponseEntity.ok(row.toString());
}
catch (Exception e)
{
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + e.getMessage());
}
}
}
This works fine when tested with Postman (returns expected data).
Consuming the REST API in .NET Core: I am trying to call this API from my .NET Core service using HttpClient
:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
string baseUrl = "http://localhost:8080/griddb"; // Java API URL
string container = "sensorData";
string key = "device***";
using HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync($"{baseUrl}/get/{container}/{key}");
if (response.IsSuccessStatusCode)
{
string data = await response.Content.ReadAsStringAsync();
Console.WriteLine("Received Data: " + data);
}
else
{
Console.WriteLine("Failed to retrieve data. Status: " + response.StatusCode);
}
}
}
Issues I am facing:
- When I call the API using Postman, it returns data correctly
- But when I call it from .NET Core using
HttpClient
, I get (mention actual error message here, like timeout, 404, 500, etc.) - I have checked the API URL and it is correct
What I have tried:
- Tested with Postman → works fine.
- Tried
HttpClient
with different headers and settings - Checked if the Java API is running and accessible from a browser
- Tried increasing timeout settings
Questions
- What could be the reason my .NET Core service is unable to get a response from the GridDB REST API, even though Postman works?
- Do I need to handle any specific CORS, firewall, or networking issues?
- Is there a better way to connect .NET Core with GridDB?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744333778a4569013.html
评论列表(0条)