用JAVA模擬POST傳送資料

2021-04-02 11:06:54 字數 1922 閱讀 9397

伺服器:

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-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++) }

往伺服器組織傳送資料

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();

socket 模擬POST傳送資料

筆者之前使用過mfc的cinternetsession來模擬post傳送資料,但一直在想,mfc中的類都是經過封裝的,如果我自己通過最原始的socket傳送資料是不是可行呢?所以經過一 早上的努力,還是成功了,說明這也不難哈。我先說說自己的過程吧 首先要構造自己的http頭部,那麼首先要看標準的ht...

curl 模擬POST方法 傳送資料

用post方法傳送資料 當發起get請求時,資料可以通過 查詢字串 query string 傳遞給乙個url。例如,在google中搜尋時,搜尋關鍵即為url的查詢字串的一部分 這種情況下你可能並不需要curl來模擬。把這個url丟給 file get contents 就能得到相同結果。不過有一...

curl模擬傳送post請求

curl模擬傳送post請求 初始化 curl curl init 設定抓取的url curl setopt curl,curlopt url,設定標頭檔案的資訊作為資料流輸出 curl setopt curl,curlopt header,1 設定獲取的資訊以檔案流的形式返回,而不是直接輸出。cu...