java post提交方式_java类的POST提交方式-可代替submit按钮

package com.nxt.portal.framework.controller.action.subscribe;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.ObjectOutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

import java.util.ArrayList;

import java.util.List;

public class ERPXml {

public String getXml(String url,List list) throws Exception{

URL urls = new URL(url);

URLConnection rulConnection = urls.openConnection();

HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;

httpUrlConnection.setDoOutput(true);

httpUrlConnection.setDoInput(true);

httpUrlConnection.setUseCaches(false);

httpUrlConnection.setRequestMethod("POST");

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

//传输字符串

/*String param="username="+java.net.URLEncoder.encode(employeeId);

System.out.println(param);

OutputStreamWriter outStrm = new OutputStreamWriter (httpUrlConnection.getOutputStream(),"UTF-8");

outStrm.flush();

outStrm.write(param);

outStrm.close();*/

//传输对象,开始写入数据

ObjectOutputStream oos = new ObjectOutputStream(httpUrlConnection.getOutputStream());

oos.writeObject(list);

oos.flush();//这句一定要写

oos.close();

InputStream inStrm = httpUrlConnection.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(inStrm));

String read = "";

StringBuffer strLine=new StringBuffer();

while ((read = br.readLine()) != null) {

strLine.append(read);

}

br.close();

inStrm.close();

httpUrlConnection.disconnect();

return strLine.toString();

}

public static void main(String[] args) throws Exception{

List list = new ArrayList();

list.add(list);

String employeeid ="LiGuo";

//另一个类的一个servlet

String url="

http://localhost:8080/wc/PostManager ";      ERPXml erp=new ERPXml();   System.out.println(erp.getXml(url,list)); } //客户端,这边需要说明,传输过来的list对象里面装的是什么对象,或者list的结构是什么样的! /*public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {     ObjectInputStream ois = new ObjectInputStream(request.getInputStream());//request.getInputStream()   List list = null;   try {    Object obj = ois.readObject();    if(obj!=null){     list = (List)obj;     System.err.println(obj);    }   } catch (ClassNotFoundException e) {    e.printStackTrace();   }   System.err.println(list);   //String uname = request.getParameter("username");   //System.err.println("uname=/t"+uname);      PrintWriter pw = response.getWriter();   pw.write("OK"); }*/ }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值