日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 发送数据_用JAVA模拟POST发送数据

發布時間:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 发送数据_用JAVA模拟POST发送数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

服務器:

1.jsp

客戶端:

首先創建一個到服務器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();

總結

以上是生活随笔為你收集整理的java 发送数据_用JAVA模拟POST发送数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。