<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 100  文章 - 50  trackbacks - 0
    <2014年12月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    我收藏的一些文章!

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    HTTP POST請求最常見的用途是發(fā)送表單參數(shù)到服務(wù)器。除了發(fā)送表單數(shù)據(jù),還可以使用POST的消息Body體發(fā)送各種數(shù)據(jù)(如純文本、XML文檔等)。本文講述如何用Java將數(shù)據(jù)寫入POST請求的Body體,已經(jīng)在servlet獲取傳過來的數(shù)據(jù)。
    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{
      //發(fā)起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");//這行很重要,如果不設(shè)置的話,后面解析xml流的話,會報:Caused by: org.xml.sax.SAXParseException: Premature end of file.

       // 得到請求的輸出流對象 
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); 
            // 把數(shù)據(jù)寫入請求的Body 
            out.write("<?xml version='1.0' encoding='UTF-8'?><trans><tran><requestId>111111</requestId><orderNo>22222</orderNo></tran><tran><requestId>aaaa</requestId><orderNo>bbbb</orderNo></tran><tran><requestId>cccccc</requestId><orderNo>ddddd</orderNo></tran></trans>"); 
      out.flush();
      out.close();

      //接收發(fā)起請求后由服務(wù)端返回的結(jié)果
      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());
     }
    }

    獲取前段發(fā)過來的post數(shù)據(jù)

    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);// 這里是前臺發(fā)起的所有參數(shù)的值,包括二進(jìn)制形式的文件和其它形式的文件
       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 閱讀(6533) 評論(0)  編輯  收藏
    主站蜘蛛池模板: 国偷自产一区二区免费视频| 亚洲av无码专区在线电影| 美女隐私免费视频看| 国产精品69白浆在线观看免费| 成人a视频片在线观看免费| 亚洲黄色三级网站| 免费A级毛片无码专区| 九月丁香婷婷亚洲综合色| a级毛片免费观看网站| 亚洲日韩国产一区二区三区| 色www免费视频| 亚洲Aⅴ无码一区二区二三区软件 亚洲AⅤ视频一区二区三区 | 亚洲综合色自拍一区| 免费观看四虎精品成人| 亚洲精品无码AV中文字幕电影网站| 边摸边吃奶边做爽免费视频网站 | 亚洲精品国产综合久久一线| 日本一区二区三区在线视频观看免费 | 99热在线精品免费全部my| 亚洲国产精品张柏芝在线观看| 久久国产精品成人片免费| 亚洲精品**中文毛片| 桃子视频在线观看高清免费完整| 在线观看黄片免费入口不卡| 国产亚洲老熟女视频| 国产午夜无码精品免费看动漫| 亚洲国产一区二区a毛片| 成人浮力影院免费看| 中文字幕亚洲综合小综合在线| 在线免费观看一级毛片| 久久精品国产亚洲AV天海翼| 亚洲av无码不卡私人影院| ssswww日本免费网站片| 亚洲国产成人久久综合碰碰动漫3d| 久久久久高潮毛片免费全部播放 | 污污视频网站免费观看| 国产亚洲一区二区手机在线观看 | 免费A级毛片无码久久版| a级毛片毛片免费观看久潮喷| 久久精品国产亚洲av麻豆小说| 成人au免费视频影院|