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

          服務(wù)器:
          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>
          客戶端:
          首先創(chuàng)建一個(gè)到服務(wù)器http的請(qǐng)求
          HttpRequest request = new HttpRequest("http://服務(wù)器/1.jsp");
          第一次使用的是GET方式
          request.setMethod("GET");
          緊接著進(jìn)行一些請(qǐng)求的屬性設(shè)置
          request.setRequestHeader("Cache-Control", "no-cache");
          這里保持連接,因?yàn)楹竺孢€要發(fā)送數(shù)據(jù)到服務(wù)器呢
          request.setRequestHeader("Connection", "Keep-Alive");
          下面是一些無(wú)關(guān)緊要的屬性設(shè)置了。
          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://服務(wù)器/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)");
          構(gòu)造好了連接請(qǐng)求,然后連接
          request.connect();
          緊接著提取Cookie值,在后文的post中可以用到。
          String strCookie = request.getResponseHeader("Set-Cookie");
          strCookie = strCookie.substring(0,strCookie.indexOf(";"));
          下面通過(guò)循環(huán)查找,提取__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;
          }
          }
          往服務(wù)器組織發(fā)送數(shù)據(jù)
          DataOutputStream dos = new DataOutputStream(request.getOutputStream());
          dos.writeBytes("-----------------------------"+strBoundary);//這是每個(gè)要被發(fā)送數(shù)據(jù)間的間隔
          dos.writeBytes(" Content-Disposition: form-data; name="__VIEWSTATE"");
          dos.writeBytes(" "+strValue);
          dos.writeBytes(" -----------------------------"+strBoundary);
          這里面是發(fā)送文件的部分
          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 閱讀(2267) 評(píng)論(1)  編輯  收藏

          FeedBack:
          # re: 用JAVA模擬POST發(fā)送數(shù)據(jù)
          2006-12-20 09:36 | 王建平
          你的HttpRequest 在那個(gè)包里面?
            回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          主站蜘蛛池模板: 忻州市| 曲靖市| 鸡东县| 罗江县| 云林县| 尉犁县| 南平市| 阿尔山市| 庄浪县| 内黄县| 皮山县| 嘉兴市| 吐鲁番市| 闵行区| 湄潭县| 皋兰县| 罗甸县| 鹤山市| 阳东县| 彭州市| 东乌珠穆沁旗| 尉犁县| 剑川县| 洞口县| 九寨沟县| 新建县| 枣强县| 山阴县| 和政县| 华容县| 巴彦县| 大港区| 内乡县| 沁源县| 枣阳市| 渭源县| 西盟| 晋州市| 金秀| 新沂市| 靖宇县|