「java调用post」java调用post接口传json

博主:adminadmin 2022-12-05 02:18:08 66

本篇文章给大家谈谈java调用post,以及java调用post接口传json对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 怎样响应post请求

Http请求类

package wzh.Http;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.URL;

import java.net.URLConnection;

import java.util.List;

import java.util.Map;

public class HttpRequest {

/**

* 向指定URL发送GET方法的请求

*

* @param url

* 发送请求的URL

* @param param

* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。

* @return URL 所代表远程资源的响应结果

*/

public static String sendGet(String url, String param) {

String result = "";

BufferedReader in = null;

try {

String urlNameString = url + "?" + param;

URL realUrl = new URL(urlNameString);

// 打开和URL之间的连接

URLConnection connection = realUrl.openConnection();

// 设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 建立实际的连接

connection.connect();

// 获取所有响应头字段

MapString, ListString map = connection.getHeaderFields();

// 遍历所有的响应头字段

for (String key : map.keySet()) {

System.out.println(key + "---" + map.get(key));

}

// 定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(new InputStreamReader(

connection.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送GET请求出现异常!" + e);

e.printStackTrace();

}

// 使用finally块来关闭输入流

finally {

try {

if (in != null) {

in.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

return result;

}

/**

* 向指定 URL 发送POST方法的请求

*

* @param url

* 发送请求的 URL

* @param param

* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。

* @return 所代表远程资源的响应结果

*/

public static String sendPost(String url, String param) {

PrintWriter out = null;

BufferedReader in = null;

String result = "";

try {

URL realUrl = new URL(url);

// 打开和URL之间的连接

URLConnection conn = realUrl.openConnection();

// 设置通用的请求属性

conn.setRequestProperty("accept", "*/*");

conn.setRequestProperty("connection", "Keep-Alive");

conn.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 发送POST请求必须设置如下两行

conn.setDoOutput(true);

conn.setDoInput(true);

// 获取URLConnection对象对应的输出流

out = new PrintWriter(conn.getOutputStream());

// 发送请求参数

out.print(param);

// flush输出流的缓冲

out.flush();

// 定义BufferedReader输入流来读取URL的响应

in = new BufferedReader(

new InputStreamReader(conn.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送 POST 请求出现异常!"+e);

e.printStackTrace();

}

//使用finally块来关闭输出流、输入流

finally{

try{

if(out!=null){

out.close();

}

if(in!=null){

in.close();

}

}

catch(IOException ex){

ex.printStackTrace();

}

}

return result;

}

}

调用方法:

public static void main(String[] args) {

//发送 GET 请求

String s=HttpRequest.sendGet("", "key=123v=456");

System.out.println(s);

//发送 POST 请求

String sr=HttpRequest.sendPost("", "key=123v=456");

System.out.println(sr);

}

java调用基于http+post+xml接口

1、直接用servlet就可以了,request.getInputStream(),然后解析xml,然后你的业务操作,组装XML,response.getOutputStream()写出去就OK了。

但这个性能低,而且还要依赖web容器。

2、socket实现http协议,把HTTP协议好好看看,自己解析(其实就是字符串的操作哦)。

3、你要是只做客户端的话可以用httpClient,几行代码搞定了

java中怎样用post,get,put请求

java中用post,get,put请求方法:

public static String javaHttpGet(String url,String charSet){

String resultData = null;

try {

URL pathUrl = new URL(url); //创建一个URL对象

HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection(); //打开一个HttpURLConnection连接

urlConnect.setConnectTimeout(30000); // 设置连接超时时间

urlConnect.connect();

if (urlConnect.getResponseCode() == 200) { //请求成功

resultData = readInputStream(urlConnect.getInputStream(), charSet);

}

} catch (MalformedURLException e) {

LogL.getInstance().getLog().error("URL出错!", e);

} catch (IOException e) {

LogL.getInstance().getLog().error("读取数据流出错!", e);

}

return resultData;

}

public static String javaHttpPost(String url,MapString,Object map,String charSet){

String resultData=null;

StringBuffer params = new StringBuffer();

try {

IteratorEntryString, Object ir = map.entrySet().iterator();

while (ir.hasNext()) {

Map.EntryString, Object entry = (Map.EntryString, Object) ir.next();

params.append(URLEncoder.encode(entry.getKey(),charSet) + "=" + URLEncoder.encode(entry.getValue().toString(), charSet) + "");

}

byte[] postData = params.deleteCharAt(params.length()).toString().getBytes();

URL pathUrl = new URL(url); //创建一个URL对象

HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection();

urlConnect.setConnectTimeout(30000); // 设置连接超时时间

urlConnect.setDoOutput(true); //post请求必须设置允许输出

urlConnect.setUseCaches(false); //post请求不能使用缓存

urlConnect.setRequestMethod("POST"); //设置post方式请求

urlConnect.setInstanceFollowRedirects(true);

urlConnect.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset="+charSet);// 配置请求Content-Type

urlConnect.connect(); // 开始连接

DataOutputStream dos = new DataOutputStream(urlConnect.getOutputStream()); // 发送请求参数

dos.write(postData);

dos.flush();

dos.close();

if (urlConnect.getResponseCode() == 200) { //请求成功

resultData = readInputStream(urlConnect.getInputStream(),charSet);

}

} catch (MalformedURLException e) {

LogL.getInstance().getLog().error("URL出错!", e);

} catch (IOException e) {

LogL.getInstance().getLog().error("读取数据流出错!", e);

} catch (Exception e) {

LogL.getInstance().getLog().error("POST出错!", e);

}

return resultData;

}

java语言使用post方式调用webService方式

WebService可以有Get、 Post、Soap、Document四种方式调用,以下Java通过post方式调用WebService代码:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.URL;

import java.net.URLConnection;

import java.net.URLEncoder;

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**

 * 功能描述:WebService调用

 * 

 */

public class ClientTest {

 /**

  * 功能描述:HTTP-POST

  * 

  */

 public String post() {

  OutputStreamWriter out = null;

  StringBuilder sTotalString = new StringBuilder();

  try {

   URL urlTemp = new URL(

     "");

   URLConnection connection = urlTemp.openConnection();

   connection.setDoOutput(true);

   out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

   StringBuffer sb = new StringBuffer();

   sb.append("byProvinceName=福建");

   out.write(sb.toString());

   out.flush();

   String sCurrentLine;

   sCurrentLine = "";

   InputStream l_urlStream;

   l_urlStream = connection.getInputStream();// 请求

   BufferedReader l_reader = new BufferedReader(new InputStreamReader(

     l_urlStream));

   while ((sCurrentLine = l_reader.readLine()) != null) {

    sTotalString.append(sCurrentLine);

   }

  } catch (Exception e) {

   e.printStackTrace();

  } finally {

   if (null != out) {

    try {

     out.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

   }

  }

  return sTotalString.toString();

 }

}

Java调用webservice和postmain调用的区别

区别是WebService可以有Get、Post、Soap、Document四种方式调用。

我们可以把webservice看做是web服务器上的一个应用,web服务器是webservice的一个容器。通过wximport生成代码。通过客户端编程方式。

通过URLConnection方式调用。

java调用post的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java调用post接口传json、java调用post的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-05,除非注明,否则均为首码项目网原创文章,转载请注明出处。