zoukankan      html  css  js  c++  java
  • java调用restful webservice(转)

    一般来说,大家只会用到GET和POST方法来调用。

    •    GET方法的话,参数可以写在url里面。

    比如说server的interface用的是@RequestParam或者@PathVariable,在客户端调用的时候,都可以直接写在URL里,具体写法我就不写了,和下面差不多。

    • POST方法。这个有点不一样,参数要写在request 的body里面,而不是URL里面。

    URL = url = new URL(http://test.webservice.api/test);

    HttpURLConnection connection = (HttpURLConnection)url.openConnection();

    connection.setDoOutput(true);

    connection.setDoInput(true);

    connection.setRequestMethod("POST");

    connection.setUseCaches(false);

    connection.setRequestProperty("Accept-Charset", "UTF-8");

    connection.setRequestProperty("Expect", "100-Continue");

    ...

    DataOutPutStream wr = new DataOutPutStream();
    wr.writeBytes("parameter=xxx¶meter2=yyy");

    wr.flush();

    wr.close();.

    ....

    参考文献http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily

    http://blog.csdn.net/j2ee_me/article/details/8848403

  • 相关阅读:
    浅谈Semaphore类
    Python浅谈requests三方库
    191104
    191103
    191102
    191101
    191031
    191030
    191029
    191028
  • 原文地址:https://www.cnblogs.com/softidea/p/5585588.html
Copyright © 2011-2022 走看看