日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/15 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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发送数据的全部內容,希望文章能夠幫你解決所遇到的問題。

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