konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks

          服務器:
          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 2007-03-07 20:14 konhon 優華 閱讀(4276) 評論(1)  編輯  收藏

          Feedback

          # re: 用JAVA模擬POST發送數據 [未登錄] 2009-05-12 03:42 ck
          HttpRequest是什么東西呢?  回復  更多評論
            


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


          網站導航:
           
          主站蜘蛛池模板: 怀集县| 营口市| 昆山市| 海丰县| 阳西县| 施秉县| 辽宁省| 黔西县| 甘肃省| 望谟县| 滨州市| 抚宁县| 盖州市| 曲沃县| 赣榆县| 朝阳县| 垣曲县| 通道| 西乡县| 贞丰县| 涡阳县| 石城县| 湘西| 安义县| 手机| 海原县| 寻乌县| 子洲县| 太和县| 嘉峪关市| 枣庄市| 斗六市| 静安区| 揭东县| 平顺县| 嵊泗县| 商丘市| 桂平市| 云阳县| 永德县| 镇康县|