java 获取post_获取POST数据的值

HTTP POST请求最常见的用途是发送表单参数到服务器。除了发送表单数据,还可以使用POST的消息Body体发送各种数据(如纯文本、XML文档等)。本文讲述如何用Java将数据写入POST请求的Body体,已经在servlet获取传过来的数据。package login;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;

import java.net.URL;

public class RequestPostTest {

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

//发起post请求

String urlString="http://localhost:8080/PostDemo/login";

URL connectURL = new URL(urlString);

HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();

conn.setReadTimeout(100000);

conn.setConnectTimeout(100000);

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setUseCaches(false);

conn.setRequestMethod("POST");

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

conn.setRequestProperty("content-type", "text/html");//这行很重要,如果不设置的话,后面解析xml流的话,会报:Caused by: org.xml.sax.SAXParseException: Premature end of file.

// 得到请求的输出流对象

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

// 把数据写入请求的Body

out.write("<?xml version='1.0' encoding='UTF-8'?>11111122222aaaabbbbccccccddddd");

out.flush();

out.close();

//接收发起请求后由服务端返回的结果

int read;

StringBuffer inputb = new StringBuffer();

InputStream is = conn.getInputStream();

InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8");

while ((read=inputStreamReader.read())>=0) {

inputb.append( (char) read);

}

System.out.println(inputb.toString());

}

}

获取前段发过来的post数据

package login;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = -5376047309978396611L;

public void service(HttpServletRequest req, HttpServletResponse res)

throws IOException {

this.doPost(req,res);

}

public void doGet(HttpServletRequest request, HttpServletResponse res) throws IOException{

this.doPost(request,res);

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{

// 测试

try {

response.setContentType("text/html; charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("SUCCESS");

ServletInputStream in = request.getInputStream();

String str = readLine(in);// 这里是前台发起的所有参数的值,包括二进制形式的文件和其它形式的文件

out.println(str);

out.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return;

}

/**

* Read the next line of input.

*

* @return a String containing the next line of input from the stream, or

*         null to indicate the end of the stream.

* @exception IOException

*                if an input or output exception has occurred.

*/

private String readLine(ServletInputStream in) throws IOException {

byte[] buf = new byte[8 * 1024];

StringBuffer sbuf = new StringBuffer();

int result;

// String line;

do {

result = in.readLine(buf, 0, buf.length); // does +=

if (result != -1) {

sbuf.append(new String(buf, 0, result, "UTF-8"));

}

} while (result == buf.length); // loop only if the buffer was filled

if (sbuf.length() == 0) {

return null; // nothing read, must be at the end of stream

}

// Cut off the trailing \n or \r\n

// It should always be \r\n but IE5 sometimes does just \n

int len = sbuf.length();

if (sbuf.charAt(len - 2) == '\r') {

sbuf.setLength(len - 2); // cut \r\n

} else {

sbuf.setLength(len - 1); // cut \n

}

return sbuf.toString();

}

}

posted on 2014-12-10 23:30 fly 阅读(6067) 评论(0)  编辑  收藏

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值