Push data by java and submit data by java proxy post

Source: Internet
Author: User

Push data in java and submit data in proxy post
On the Internet .. Leave backup.

[Java]
Import org. apache. commons. httpclient .*;
Import org. apache. commons. httpclient. methods. GetMethod;
Import org. apache. commons. httpclient. methods. PostMethod;
Import org. apache. commons. httpclient. params. HttpClientParams;
Import org. apache. commons. httpclient. params. HttpMethodParams;


Import java. awt. image. BufferedImage;

Import java. io. InputStream;

Import javax. imageio. ImageIO;


Public class MyHttpClient {

/***//**
* Method description
*
*
* @ Param title: Message title
* @ Param name: message recipient
* @ Param Content
* @ Param proIP proxy IP
* @ Param port proxy port
* @ Param usePro whether to use a proxy
*/
Public synchronized void doSomeThing (String title, String name, String Content, String proIP, int port,
Boolean usePro ){

// Construct an HttpClient instance
HttpClient httpClient = new HttpClient ();
HttpClientParams clientParams = new HttpClientParams ();

// Hide information related to your request
ClientParams. setParameter ("http. useragent", "Mozilla/4.0 (compatible; FIREFOX 9.0; ibm aix 5 )");

// HttpClient. getHttpConnectionManager (). getParams (). setSoTimeout (30*1000 );
ClientParams. setHttpElementCharset ("GBK ");

HttpState httpState = new HttpState ();

HttpClient. setParams (clientParams );
HttpClient. getParams (),. setParameter (HttpClientParams. HTTP_CONTENT_CHARSET, "GBK ");
HttpClient. setState (httpState );
ClientParams. setVersion (HttpVersion. HTTP_1_1 );

// HttpClient. getHostConfiguration (). setProxy ("148.233.15 9.58", 3128 );

If (usePro) // use a proxy
...{
HttpClient. getHostConfiguration (). setProxy (proIP, port );
}

// Create an instance of the GET Method
GetMethod getMethod = new GetMethod ("http://www.XXXcom/Guestbook/imgchk/validatecode.asp ");

// Use the default recovery policy provided by the system
GetMethod. getParams (). setParameter (HttpMethodParams. RETRY_HANDLER, new DefaultHttpMethodRetryHandler ());

Try ...{

// Execute getMethod
Int statusCode = httpClient.exe cuteMethod (getMethod );

// System. out. println (statusCode );
If (statusCode! = HttpStatus. SC _ OK ){
System. err. println ("Method failed:" + getMethod. getStatusLine ());
} // Read content

InputStream inStream = getMethod. getResponseBodyAsStream ();

// Process content
// System. out. println (new String (responseBody ));
BufferedImage iag = ImageIO. read (inStream );
ImgIdent imgIdent = new ImgIdent (iag );

// ImgIdent. saveJPEG (iag, "C:/ddd.jpg ");
String validate = imgIdent. getValidatecode (4 );

System. out. println (validate );

PostMethod method = new PostMethod ("http://www.XXX.com/Guestbook/add_msg.asp ");
String connect = Content;
String Title = title;

Method. setParameter ("subject", Title );
Method. setParameter ("g_name", name );
Method. setParameter ("companyname ","");
Method. setParameter ("mail ","");
Method. setParameter ("homepageurl", "http ://");
Method. setParameter ("pic", "p5.gif ");
Method. setParameter ("validatecode", validate );
Method. setParameter ("content", connect );

// If (todo ){
Int code = httpClient.exe cuteMethod (method );

// String Stringresponse = new String (method. getResponseBodyAsString (). getBytes ("8859_1 "));
// Print the returned information
// System. out. println (Stringresponse );
//}

Method. releaseConnection ();

// System. out. println (iag. getHeight ());
// System. out. println (iag. getWidth ());
/// Background color
// IntBgColor = iag. getRGB (38, 0 );
// System. out. println ("intBgColor =" + intBgColor );
//
//
// IntBgColor = iag. getRGB (0, 0 );
// System. out. println ("intBgColor =" + intBgColor );
} Catch (Exception e ){

// Network exception
E. printStackTrace ();
} Finally {}

// Release the connection getMethod. releaseConnection ();}
GetMethod. releaseConnection ();
}
}

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.