如何从Java类向Web Api 2发送发帖请求

时间:2019-07-17 11:44:50

标签: java http-post

我想通过发布请求将数据从Java类发送到C#Web Api 2项目。但我无法发送。我尝试了很多方法。虽然我可以发送get请求,但我的发布请求的结果始终返回404 Not Found。我已经在服务器端配置了CORS设置。我的帖子请求是;

Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://localhost:52729/").path("GetResponse");

DenemeModel model = new DenemeModel();
model.Name = "Cem";

Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);
try {
    Response response1 = invocationBuilder.post(Entity.entity(model, MediaType.APPLICATION_JSON));
    DenemeModel employee = response1.readEntity(DenemeModel.class);
    System.out.println(response.getStatus());
    System.out.println(employee);
} catch (Exception e) {
    Logger.getLogger(Deneme.class.getName()).log(Level.SEVERE, null, e.getMessage()); 
}

0 个答案:

没有答案