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 優華 閱讀(4270) 評論(1)  編輯  收藏

          Feedback

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


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


          網站導航:
           
          主站蜘蛛池模板: 隆回县| 沅江市| 商城县| 大石桥市| 厦门市| 元谋县| 巴里| 靖安县| 鸡泽县| 右玉县| 和顺县| 遵化市| 乐至县| 界首市| 汉阴县| 海伦市| 平利县| 东港市| 巨野县| 雷山县| 汤原县| 琼海市| 玉龙| 耒阳市| 嘉义市| 安平县| 海城市| 乐安县| 莲花县| 沙洋县| 沂南县| 广州市| 遂平县| 寻乌县| 德安县| 张家口市| 中西区| 来凤县| 肥东县| 卓尼县| 揭东县|