posts - 23,comments - 66,trackbacks - 0
          from:http://blog.csdn.net/ambitiontan/archive/2006/01/05/571272.aspx

          服務器:
          1.jsp
          <body>
          <form name="_ctl0" method="post" action="TestFileManager.aspx" id="_ctl0" enctype="multipart/form-data">
          <input type="hidden" name="__VIEWSTATE" value="dDwyNTIzNjA5NDU7Oz7rsE3eBYzQHDVtl+aTn96MvQW6PQ==" />
          <p>
          <input name="uploadfile1" id="uploadfile1" type="file" size="49" />
          <input type="submit" name="Button1" value="?" id="Button1" />
          </p>
          <p>
          <span id="Label1" style="width:459px;"></span>
          </p>
          <!-- Insert content here -->
          </form>
          </body>
          客戶端:
          首先創建一個到服務器http的請求
          HttpRequest request = new HttpRequest("http://服務器/1.jsp");
          第一次使用的是GET方式
          request.setMethod("GET");
          緊接著進行一些請求的屬性設置
          request.setRequestHeader("Cache-Control", "no-cache");
          這里保持連接,因為后面還要發送數據到服務器呢
          request.setRequestHeader("Connection", "Keep-Alive");
          下面是一些無關緊要的屬性設置了。
          request.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
          request.setRequestHeader("Accept-Encoding", "gzip, deflate");
          request.setRequestHeader("Accept-Language", "en-au");
          request.setRequestHeader("Referer", "http://服務器/1.jsp");
          request.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3215; .NET CLR 1.0.3705)");
          構造好了連接請求,然后連接
          request.connect();
          緊接著提取Cookie值,在后文的post中可以用到。
          String strCookie = request.getResponseHeader("Set-Cookie");
          strCookie = strCookie.substring(0,strCookie.indexOf(";"));
          下面通過循環查找,提取__VIEWSTATE的值
          for ( int i = 0; i < nlist.getLength(); i++) {
          node = nlist.item(i);
          strName = getNodeAttributeValue(node,"name");
          if ( strName.equals("__VIEWSTATE") ) {
          strValue = getNodeAttributeValue(node,"value");
          break;
          }
          }
          往服務器組織發送數據
          DataOutputStream dos = new DataOutputStream(request.getOutputStream());
          dos.writeBytes("-----------------------------"+strBoundary);//這是每個要被發送數據間的間隔
          dos.writeBytes(" Content-Disposition: form-data; name="__VIEWSTATE"");
          dos.writeBytes(" "+strValue);
          dos.writeBytes(" -----------------------------"+strBoundary);
          這里面是發送文件的部分
          dos.writeBytes(" Content-Disposition: form-data; name="uploadfile1"; filename="" + strFileName + """);
          dos.writeBytes(" Content-Type: text/xml");
          dos.writeBytes(" ");
          dos.writeBytes(new String(data));
          dos.writeBytes(" -----------------------------"+strBoundary);
          dos.writeBytes(" Content-Disposition: form-data; name="Button1"");
          dos.writeBytes(" 上傳");
          dos.writeBytes(" -----------------------------"+strBoundary+"--");
          dos.writeBytes(" ");
          dos.close();
          posted on 2006-03-21 21:44 rd2pm 閱讀(2263) 評論(1)  編輯  收藏

          FeedBack:
          # re: 用JAVA模擬POST發送數據
          2006-12-20 09:36 | 王建平
          你的HttpRequest 在那個包里面?
            回復  更多評論
            

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


          網站導航:
           

          主站蜘蛛池模板: 铅山县| 钟山县| 屏南县| 新兴县| 广平县| 周至县| 景宁| 襄汾县| 大宁县| 武宁县| 竹山县| 东辽县| 景宁| 敦煌市| 丘北县| 寿宁县| 侯马市| 沙湾县| 枣阳市| 稷山县| 普定县| 济宁市| 延庆县| 富源县| 柳林县| 武陟县| 乃东县| 芜湖市| 贵港市| 屯留县| 周宁县| 罗田县| 峨眉山市| 普兰店市| 泗水县| 宾阳县| 蓝山县| 罗田县| 祁门县| 山东| 岳普湖县|