Java simulates http get and post requests

Source: Internet
Author: User

Import org. Apache. commons. httpclient. httpclient;
Import org. Apache. commons. httpclient. httpmethod;
Import org. Apache. commons. httpclient. httpstatus;
Import org. Apache. commons. httpclient. uriexception;
Import org. Apache. commons. httpclient. Methods. getmethod;
Import org. Apache. commons. httpclient. Methods. postmethod;
Import org. Apache. commons. httpclient. Params. httpmethodparams;
Import org. Apache. commons. httpclient. util. uriutil;
Import org. Apache. commons. Lang. stringutils;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

 

 

Import java. Io. ioexception;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. Map;

 

 


Public final class httptookit {
Private Static log = logfactory. getlog (httptookit. Class );

 

 


Public static string doget (string URL, string querystring ){
String response = NULL;
Httpclient client = new httpclient ();
Httpmethod method = new getmethod (URL );
Try {
If (stringutils. isnotblank (querystring ))
Method. setquerystring (uriutil. encodequery (querystring ));
Client.exe cutemethod (method );
If (method. getstatuscode () = httpstatus. SC _ OK ){
Response = method. getresponsebodyasstring ();
}
} Catch (uriexception e ){
Log. Error ("when an http get request is executed, an exception occurs when the encoded query string" "+ querystring +! ", E );
} Catch (ioexception e ){
Log. Error ("executing http get request" + URL + ", an exception occurred! ", E );
} Finally {
Method. releaseconnection ();
}
Return response;
}

 

 


Public static string dopost (string URL, Map <string, string> Params ){
String response = NULL;
Httpclient client = new httpclient ();
Httpmethod method = new postmethod (URL );
For (iterator it = Params. entryset (). iterator (); it. hasnext ();){

 

 

}
// Set http post data
If (Params! = NULL ){
Httpmethodparams P = new httpmethodparams ();
For (Map. Entry <string, string> entry: Params. entryset ()){
P. setparameter (entry. getkey (), entry. getvalue ());
}
Method. setparams (P );
}
Try {
Client.exe cutemethod (method );
If (method. getstatuscode () = httpstatus. SC _ OK ){
Response = method. getresponsebodyasstring ();
}
} Catch (ioexception e ){
Log. Error ("executing http post request" + URL + ", an exception occurred! ", E );
} Finally {
Method. releaseconnection ();
}

 

 

Return response;
}

 

 

Public static void main (string [] ARGs ){
String x = dopost ("http://lavasoft.blog.51cto.com/62575/64994", new hashmap ());
System. Out. println (X );
}
}
 
The output is as follows:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> use regular expressions in Java to verify the string-fuyan-51cto technology blog </title>
<Meta name = "Description" content = "using regular expressions in Java to verify string regular expressions was invented by a great mathematician and has now formed an ISO standard, this standard has nothing to do with programming languages. I also forgot who invented it and how it was invented :). Regular Expressions are simply understood as... ">
<Meta name = "keywords" content = "using regular expressions in Java to verify strings">
<Meta http-equiv = "cache-control" content = "private">
<Base href = "http://lavasoft.blog.51cto.com/"> </base>
<SCRIPT src = "/JS/DEF. js"> </SCRIPT>
<Script language = JavaScript src = "http://blog.51cto.com/js/message.js" type = text/JavaScript> </SCRIPT>
<Script language = JavaScript src = "http://blog.51cto.com/js/user_comment.js" type = text/JavaScript> </SCRIPT>
<Script language = JavaScript src = "http://blog.51cto.com/js/base2.js" type = text/JavaScript> </SCRIPT>
<Script language = JavaScript src = "http://blog.51cto.com/js/dialog_utf8.js" type = text/JavaScript> </SCRIPT>
<Link href = "/CSS/skin/2.css" rel =" stylesheet "type =" text/CSS ">
<Link href = "/CSS/group/group.css" rel = "stylesheet" type = "text/CSS">
<LINK rel = "alternate" href = "../RSS. php? Uid = 62575 "type =" application/RSS + XML "Title =" RSS 2.0 ">
<LINK rel = "edituri" type = "application/RSD + XML" Title = "RSD" href = "XMLRPC. php? RSD = 1 "/>
<SCRIPT>
VaR myid = "";
Function add_flink (){
If (myid ){
VaR url = '/MoD/edit_flink.php? Type = addflink & uid = 62575 & flink = http://lavasoft.blog.51cto.com ';
VaR Ajax = initajax1 ();
Ajax. Open ("get", URL, true );
Ajax. onreadystatechange = function (){
If (Ajax. readystate = 4 & Ajax. Status = 200 ){
If (Ajax. responsetext = ""){
Alert ("added successfully. ");
}
If (Ajax. responsetext = "1 "){
Alert ("link pointing to yourself. ");
}
If (Ajax. responsetext = "2 "){
Alert ("The Link already exists. ")
}
}
}
Ajax. Send (null );
} Else {

 

 

VaR refurlk = "http://lavasoft.blog.51cto.com/62575/64994 ";

 

 

Commentsubmit ("", refurlk );
Return false;
}

}
Function sendmessage (){

 

 

VaR refurlk = "http://lavasoft.blog.51cto.com/62575/64994 ";

 

 

If (myid ){
Return true;
} Else {
Commentsubmit ("", refurlk );
Return false;
}
}
Function copylink (ourl ){
If (! Ourl ){
VaR clipboardcontent = "http://lavasoft.blog.51cto.com ";
} Else {
VaR clipboardcontent = ourl;
}
Window. clipboardData. setdata ("text", clipboardcontent );
Alert ("Copied successfully! ");
Return false;
}
Function correctpng (){
If (document. getelementbyid ('blog _ touxian '))
{
VaR IMG = Document. getelementbyid ('blog _ touxian ');
VaR imgname = IMG. SRC. touppercase ()
VaR imgid = (IMG. ID )? "Id = '" + IMG. ID + "'":""
VaR imgclass = (IMG. classname )? "Class = '" + IMG. classname + "'":""
VaR imgtitle = (IMG. Title )? "Title = '" + IMG. Title + "'": "Title = '" + IMG. Alt + "'"
VaR imgstyle = "display: inline-block;" + img.style.css text
If (IMG. align = "Left") imgstyle = "float: Left;" + imgstyle
If (IMG. align = "right") imgstyle = "float: right;" + imgstyle
If (IMG. parentelement. href) imgstyle = "cursor: hand;" + imgstyle
VaR strnewhtml = "<span" + imgid + imgclass + imgtitle
+ "Style =/" "+" width: "+ IMG. Width +" PX; Height: "+ IMG. height +" PX; "+ imgstyle + ";"
+ "Filter: progid: DXImageTransform. Microsoft. alphaimageloader"
+ "(Src =/'" + IMG. SRC + "/', sizingmethod = 'Scale');/"> </span> ";
IMG. outerhtml = strnewhtml;
}
}
// Window. attachevent ("correctpng );
Window.
</SCRIPT>
<Style type = "text/CSS">
# Layout_0 {
Width :;
Float: left;
}
# Layout_1 {
Width :;
Float: left;
}
# Layout_3 {
Width :;
Float: left;
}
. Albumalert {
Width: 250px;
Height: 150px;
Background: # FFF;
Border: 1px solid #777;
}
. Alerttitle {
Text-align: left;
Color: #000;
Padding: 6px;
Background-image: URL ("http://img1.51cto.com/images/quickwindow.jpg ");
Background-repeat: Repeat-X;
Font-size: 14px;
Height: 22px;
}
. Alertcentent {
Line-Height: 30px;
}
. Alertcentent P {
Color: #000;
}
 
The above Code omitted n multiple lines.
 
In addition, I would like to give another example of a bot program written by a friend to spoof Feixin fraud websites. You are welcome to run a lot and stop these scammers:
Import java. Io. bufferedreader;
Import java. Io. bufferedwriter;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstreamwriter;
Import java.net. httpurlconnection;
Import java.net. protocolexception;
Import java.net. url;

 

 

Import com. Verisign. UUID. UUID;

 

 


Public class autosubmit {

 

 

 
Public static void main (string [] ARGs) throws exception {
For (INT I = 0; I <100000; I ++ ){
Post (I );
}
}

Private Static void post (int I) throws exception {
String S = UUID. Generate (). tostring ();
String S1 = S. substring (0, 2 );
S = S1 + S. substring (S. Length ()-3, S. Length ());

URL url = new URL ("http://yfs88.sv10.sgedns.cn/yy/e/qq22.asp"); // submit the address
Httpurlconnection = (httpurlconnection) URL. openconnection ();
Httpurlconnection. setdooutput (true); // open the write attribute
Httpurlconnection. setdoinput (true); // open the read attribute
Httpurlconnection. setrequestmethod ("Post"); // sets the submission method.
Httpurlconnection. setconnecttimeout (50000); // connection timeout
Httpurlconnection. setread timeout (50000 );
Httpurlconnection. Connect ();

Bufferedwriter out = new bufferedwriter (New outputstreamwriter (httpurlconnection. getoutputstream (), "GBK "));
Out. Write ("name =" + S + I + // the user name cannot be repeated.
"& Password = 748" +
"& Sex = very good for you" +
"& OICQ = 748748" +
"& ICQ = 748748" +
"& MSN = caonima" +
"& Shengri = don't close the door, close all of you." +
"& Home = already targeting you"); // data to be post, separated &
Out. Flush ();
Out. Close ();

 

 

 

 

// Read the returned value after post
// Bufferedreader in = new bufferedreader (New inputstreamreader (inputstream) httpurlconnection. getinputstream ()));
// String line = NULL;
// Stringbuilder sb = new stringbuilder ();
// While (line = in. Readline ())! = NULL ){
// Sb. append (line );
//}
// In. Close ();
// System. Out. println ("client:" + sb. tostring ());

Httpurlconnection. Disconnect (); // disconnect

 

 

//
System. Out. println ("client post OK:" + I );
}

 

 

}

Related Article

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.