隨筆-6  評論-1  文章-0  trackbacks-0
          URL url = new URL("提交的URL");
           HttpURLConnection connection = (HttpURLConnection) url.openConnection();
           try
           {
            connection.setDoInput(true);
            connection.setDoOutput(true);
            {
             // 提交的內容
             byte[] requsetContent = new byte[1024];
             connection.setRequestProperty("Content-Length", Integer.toString(requsetContent.length));
             OutputStream outputStream = connection.getOutputStream();
             try
             {
              // 向外輸入流
              outputStream.write(requsetContent);
              outputStream.flush();
             }
             catch (Exception ex)
             {
              throw ex;
             }
             finally
             {
              outputStream.close();
             }
            }
            // 獲取HTTP相應請求
            int responseCode = connection.getResponseCode();
            String responseMessage = connection.getResponseMessage();
            {
             ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 64);
             // 得到返回流
             InputStream inputStream = connection.getInputStream();
             try
             {
              byte[] buf = new byte[1024 * 64];
              int n;
              while ((n = inputStream.read(buf)) >= 0)
              {
               baos.write(buf, 0, n);
              }
             }
             catch (Exception ex)
             {
              ex.printStackTrace();
             }
             finally
             {
              inputStream.close();
             }
             // 獲取包的內容
             byte[] responseContent = baos.toByteArray();
            }
           }
           catch (Exception ex)
           {
            throw ex;
           }
           finally
           {
            connection.disconnect();
           }
          posted on 2008-04-29 15:32 天堂有路 閱讀(848) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 江口县| 茌平县| 湘潭市| 喀什市| 若尔盖县| 万年县| 航空| 若羌县| 洪洞县| 伊春市| 建平县| 镇江市| 法库县| 丰台区| 改则县| 曲松县| 古蔺县| 万盛区| 弋阳县| 游戏| 马龙县| 饶河县| 彭阳县| 得荣县| 东宁县| 玉林市| 昌图县| 团风县| 和硕县| 集贤县| 石首市| 阜康市| 卢龙县| 五常市| 马鞍山市| 尤溪县| 郎溪县| 舒城县| 庆安县| 社旗县| 恩施市|