Simulate post data sending using Java

Source: Internet
Author: User
Server:
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>
Client:
First, create an http request to the server.
HttpRequest request = new HttpRequest ("http: // server/1.jsp ");
The first time I used the GET method
Request. setMethod ("GET ");
Then, set the request attributes.
Request. setRequestHeader ("cache-control", "No-Cache ");
Keep the connection here, because data will be sent to the server later.
Request. setRequestHeader ("connection", "keep-alive ");
The following are some irrelevant attribute settings.
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: // server/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 )");
Construct the connection request and connect
Request. Connect ();
Then extract the cookie value, which can be used in post.
String strcookie = request. getResponseHeader ("Set-cookie ");
StrCookie = strCookie. substring (0, strCookie. indexOf (";"));
The following uses a loop query to extract the value of _ 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;
}
}
Send data to server Organization
DataOutputStream dos = new DataOutputStream (request. getOutputStream ());
Dos. writeBytes ("-----------------------------" + strBoundary); // This is the interval between each data to be sent
Dos. writeBytes ("Content-Disposition: form-data; name =" _ VIEWSTATE "");
Dos. writeBytes ("" + strValue );
Dos. writeBytes ("-----------------------------" + strBoundary );
This is the part of the file to be sent.
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 ("Upload ");
Dos. writeBytes ("-----------------------------" + strBoundary + "--");
Dos. writeBytes ("");
Dos. close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.