struts2 中使用request.getInputStream() 获取POST数据

// 发送POST请求
	public static void getPort2(){
		try {
			String postData="{name,age,[student1,aa1,aa2],[student2,aa1,aa2]}";			
			URL url = new URL("http://127.0.0.1:9080/api/interface/getConfig.ac");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type","text/html");
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			out.write(postData);
			out.flush();
			out.close();

// 获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
            }
            // 获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            System.out.println("```````"+result);
            in.close();
          
conn.disconnect();
		} catch (IOException e) {
			e.printStackTrace(System.out);
		}		
	}

接收POST传过来的json数据,在struts中的Action方法中,调用request.getInputStream() 来读取数据。


try {
			BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
			System.out.println("portDate==="+br.readLine());	        
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

如无法读取,注意

conn.setRequestProperty("Content-Type","text/html");



=====================================================


直接get获取数据

public static void getPort1(){
		StringBuffer json = new StringBuffer();
        try {
            //实例一个url和URLConnection
            URL oracle = new URL("http://127.0.0.1:9080/api/interface/getConfig.ac");
            //打开链接
            URLConnection yc = oracle.openConnection();
            //输入流作参数传进InputStreamReader并用BufferedReader接受
            BufferedReader in = new BufferedReader(new InputStreamReader(
                                        yc.getInputStream()));
            String inputLine = null;
            //一直读到空,并设置流编码是UTF8
            while ( (inputLine = in.readLine()) != null) {
            	System.out.println("=="+inputLine);
                json.append(new String(inputLine.getBytes(),"GBK"));
            }
            //记得关闭连接
            in.close();
        } catch (Exception e)  {
            e.printStackTrace();
        }
	}



post另外一种方式


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;


public static void getPort3() {
        try {
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(
                    "http://127.0.0.1:9080/SafeSysApi/SecureServer/reportInfo.ac");
            String postData = "{\"use\": \"user1\",\"account\": \"account1\",\"UserInfo\": [{\"name\": \"xwang\",\"age\": [\"aa\",\"dd\",\"aa\"]}],\"shool\": \"eee\",\"Type\": \"china\"}";
            
            ((PostMethod) method).addParameter("UserInfos", postData);
            HttpMethodParams param = method.getParams();
            param.setContentCharset("UTF-8");
            System.out.println(param);
            client.executeMethod(method);
            // 打印服务器返回的状态
            System.out.println(method.getStatusLine());
            // 打印返回的信息
            System.out.println();
            InputStream stream = method.getResponseBodyAsStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    stream, "UTF-8"));
            StringBuffer buf = new StringBuffer();
            String line;
            while (null != (line = br.readLine())) {
                buf.append(line).append("\n");
            }
            System.out.println(buf.toString());
            // 释放连接
            method.releaseConnection();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }


对应包下载:

http://download.csdn.net/detail/wyyother1/9818669

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//接收

jsonData = getRequestParm("UserInfos");

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值