1. 程式人生 > >java推送資料,java代理post提交資料

java推送資料,java代理post提交資料

java推送資料,代理post提交資料

網上看到的。。 留下備用。

[java] view plaincopyprint?
  1. import org.apache.commons.httpclient.*;  
  2. import org.apache.commons.httpclient.methods.GetMethod;  
  3. import org.apache.commons.httpclient.methods.PostMethod;
  4. import org.apache.commons.httpclient.params.HttpClientParams;  
  5. import org.apache.commons.httpclient.params.HttpMethodParams;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.InputStream;  
  8. import javax.imageio.ImageIO;  
  9. publicclass MyHttpClient {  
  10.     /** *//**
  11.      * Method description
  12.      *
  13.      *
  14.      * @param title 留言標題
  15.      * @param name 留言者
  16.      * @param Content 內容
  17.      * @param proIP 代理IP
  18.      * @param port  代理埠
  19.      * @param usePro 是否使用代理
  20.      */
  21.     publicsynchronizedvoid doSomeThing(String title, String name, String Content, String proIP, int port,  
  22.             boolean usePro) {  
  23.         // 構造HttpClient的例項
  24.         HttpClient       httpClient   = new HttpClient();  
  25.         HttpClientParams clientParams = new
    HttpClientParams();  
  26.         // 隱藏自己請求相關的資訊
  27.         clientParams.setParameter("http.useragent", "Mozilla/4.0 (compatible; FIREFOX 9.0; IBM AIX 5)");  
  28.         // httpClient.getHttpConnectionManager().getParams().setSoTimeout(30 * 1000);
  29.         clientParams.setHttpElementCharset("GBK");  
  30.         HttpState httpState = new HttpState();  
  31.         httpClient.setParams(clientParams);  
  32.         httpClient.getParams(), .setParameter(HttpClientParams.HTTP_CONTENT_CHARSET, "GBK");  
  33.         httpClient.setState(httpState);  
  34.         clientParams.setVersion(HttpVersion.HTTP_1_1);  
  35.         // httpClient.getHostConfiguration().setProxy("148.233.159.58", 3128);
  36.         if (usePro)    // 使用代理
  37.         ...{  
  38.             httpClient.getHostConfiguration().setProxy(proIP, port);  
  39.         }  
  40.         // 建立GET方法的例項
  41.         GetMethod getMethod = new GetMethod("http://www.XXXcom/Guestbook/imgchk/validatecode.asp");  
  42.         // 使用系統提供的預設的恢復策略
  43.         getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());  
  44.         try ...{  
  45.             // 執行getMethod
  46.             int statusCode = httpClient.executeMethod(getMethod);  
  47.             // System.out.println(statusCode);
  48.             if (statusCode != HttpStatus.SC_OK) {  
  49.                 System.err.println("Method failed: " + getMethod.getStatusLine());  
  50.             }    // 讀取內容
  51.             InputStream inStream = getMethod.getResponseBodyAsStream();  
  52.             // 處理內容
  53.             // System.out.println(new String(responseBody));
  54.             BufferedImage iag      = ImageIO.read(inStream);  
  55.             ImgIdent      imgIdent = new ImgIdent(iag);  
  56.             // imgIdent.saveJPEG(iag, "C:/ddd.jpg");
  57.             String validate = imgIdent.getValidatecode(4);  
  58.             System.out.println(validate);  
  59.             PostMethod method  = new PostMethod("http://www.XXX.com/Guestbook/add_msg.asp");  
  60.             String     connect = Content;  
  61.             String     Title   = title;  
  62.             method.setParameter("subject", Title);  
  63.             method.setParameter("g_name", name);  
  64.             method.setParameter("companyname", "");  
  65.             method.setParameter("mail", "");  
  66.             method.setParameter("homepageurl", "http://");  
  67.             method.setParameter("pic", "p5.gif");  
  68.             method.setParameter("validatecode", validate);  
  69.             method.setParameter("content", connect);  
  70. //          if (todo) {
  71.             int code = httpClient.executeMethod(method);  
  72.             // String Stringresponse = new String(method.getResponseBodyAsString().getBytes("8859_1"));
  73.             // 列印返回的資訊
  74.             // System.out.println(Stringresponse);
  75. //          }
  76.             method.releaseConnection();  
  77. //          System.out.println(iag.getHeight());
  78. //          System.out.println(iag.getWidth());
  79. //          //背景 顏色
  80. //          intBgColor = iag.getRGB(38, 0);
  81. //          System.out.println("intBgColor=" + intBgColor);
  82. //
  83. //
  84. //          intBgColor = iag.getRGB(0, 0);
  85. //          System.out.println("intBgColor=" + intBgColor);
  86.         } catch (Exception e) {  
  87.             // 發生網路異常
  88.             e.printStackTrace();  
  89.         } finally {}  
  90.         // 釋放連線   getMethod.releaseConnection();  }
  91.         getMethod.releaseConnection();  
  92.     }  
  93. }