gpt4 book ai didi

java - 无法从 Java Web 服务获取数据

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:13 24 4
gpt4 key购买 nike

请有人帮助我,我一直在尝试,但无法找出我无法获得结果的原因。

我已经创建了这个 java springboot web 服务,当我运行 java 应用程序时,一个 web 浏览器页面将打开,当我输入 URL 例如 localhost:8080/runbatchfileparam/test.bat程序将首先检查 test.bat 文件是否存在。如果是,网页将显示一个 JSON 结果 {“Result”: true},批处理文件中的命令将被执行。如果不存在,网页会显示{“Result”: false}

我想创建一个 ASP.NET Web 服务,它将使用在 java Web 服务中创建的函数。当我运行 ASP.NET Web 应用程序时,将打开一个 Web 浏览器页面。用户将输入类似这样的 URL:localhost:12345/api/callbatchfile/test.bat。 Java Web 服务应该正在运行,当我运行 C# ASP.NET Web 时,我应该得到 {“Result”: true}{“Result”: false}应用也。

但是我只得到一个空的 {},括号内没有任何内容。为什么会这样?

这是我在 ASP.NET 中的代码

TestController.cs

private TestClient testClient = new TestClient();
public async Task<IHttpActionResult> GET(string fileName)
{
try
{
var result = await testClient.runbatchfile(fileName);
var resultDTO = JsonConvert.DeserializeObject<TestVariable>(result);
return Json(resultDTO);
}
catch (Exception e)
{
var result = "Server is not running";
return Ok(new { ErrorMessage = result });
}
}

测试变量.cs

public class TestVariable
{
public static int fileName { get; set; }
}

TestClient.cs

private static HttpClient client;
private static string BASE_URL = "http://localhost:8080/";

static TestClient()
{
client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}

public async Task<string> runbatchfile(string fileName)
{
var endpoint = string.Format("runbatchfile/{0}", fileName);
var response = await client.GetAsync(endpoint);
return await response.Content.ReadAsStringAsync();
}

WebApiConfig.cs

config.Routes.MapHttpRoute(
name: "TestBatchClient",
routeTemplate: "api/runbatchfile/{fileName}",
defaults: new { action = "GET", controller = "Test" }
);

有人请帮帮我。非常感谢。

编辑

Java 网络服务

应用程序.java

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

BatchFileController.java

private static final String template = "Sum, %s!";  

@RequestMapping("/runbatchfile/{param:.+}")
public ResultFormat runbatchFile(@PathVariable("param") String fileName) {
RunBatchFile rbf = new RunBatchFile();
return rbf.runBatch(fileName);
}

结果格式

private boolean result;

public ResultFormat(boolean result) {
this.result = result;
}

public boolean getResult() {
return result;
}

RunBatchFile.java

public ResultFormat runBatch(String fileName) {

String var = fileName;
String filePath = ("C:/Users/attsuap1/Desktop/" + var);
try {
Process p = Runtime.getRuntime().exec(filePath);

int exitVal = p.waitFor();

return new ResultFormat(exitVal == 0);

} catch (Exception e) {
e.printStackTrace();
return new ResultFormat(false);
}
}

最佳答案

我不确定这是否有帮助...但我怀疑 AsyncTask 并未真正执行...

var result = await testClient.testCallBatchProject(fileName);

我会尝试下面的操作:

await testClient.testCallBatchProject(fileName).Delay(1000);

您可以尝试检查同步调用是否会发生同样的情况吗? .. 如果是这样,我们可以将上述内容归零。

关于java - 无法从 Java Web 服务获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48416563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com