JAVA程序怎么调用Rest webservice

Python016

JAVA程序怎么调用Rest webservice,第1张

import java.io.ByteArrayOutputStream

import java.io.InputStream

import java.net.HttpURLConnection

import java.net.URL

import java.nio.charset.StandardCharsets

import java.util.ArrayList

import java.util.List

import org.apache.commons.codec.binary.Base64

import org.json.JSONArray

import org.json.JSONException

import org.json.JSONObject

post的改成put试试

HttpPost httpPost = new HttpPost("http xxx")

List <NameValuePair>nvps = new ArrayList <NameValuePair>()

nvps.add(new BasicNameValuePair("username", "vip"))

nvps.add(new BasicNameValuePair("password", "secret"))

httpPost.setEntity(new UrlEncodedFormEntity(nvps))

CloseableHttpResponse response2 = httpclient.execute(httpPost)

try {

System.out.println(response2.getStatusLine())

HttpEntity entity2 = response2.getEntity()

// do something useful with the response body

// and ensure it is fully consumed

EntityUtils.consume(entity2)

} finally {

response2.close()

}