[英]REST API cannot be accessed by the C# Windows Form
我已經使用Spring Boot和IntelliJ IDEA開發了REST API。 該API已啟動並正在運行。 Postman可以訪問它並提供相關的JSON響應。
然后,我嘗試從C#Windows窗體應用程序訪問它。 我試圖將其添加為網絡參考。 但是該按鈕已被禁用。 我的代碼有什么問題?
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value="/hello")
String home() {
return "Hello World!";
}
@RequestMapping(value="getAllStudents",method = RequestMethod.GET)
public Collection<Student> getAllStudents(){
return studentService.getAllStudents();
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Student getStudentById(@PathVariable("id") int id)
{
return studentService.getStudentById(id);
}
@RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
public void deleteStudentById(@PathVariable("id") int id)
{
studentService.removeStudentById(id);
}
如果我將URL設置為localhost:8080 / students / hello,則JSON響應將發送到Visual Studio。 但是不能將其添加為網絡參考。
這有什么解決方案...?
Visual Studio中的Web引用適用於SOAP / ASMX Web服務。 如果您正在調用REST API,則正如@fharreau所評論的那樣,您將需要使用HttpClient。
該鏈接顯示了如何執行此操作,但是可能很難閱讀/關注:
這樣的答案可能更好
https://stackoverflow.com/a/10928807/2309376
在SO答案中,您將需要用URL“ http:// localhost:8080 / students / hello ”替換“ http://www.contoso.com/ ”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.