java推送數據,java代理post提交數據 – JAVA編程語言程序開發技術文章

java推送數據,代理post提交數據
網上看到的。。 留下備用。

[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 留言標題 
     * @param name 留言者 
     * @param Content 內容 
     * @param proIP 代理IP 
     * @param port  代理端口 
     * @param usePro 是否使用代理 
     */  
    public synchronized void doSomeThing(String title, String name, String Content, String proIP, int port,  
            boolean usePro) {  
  
        // 構造HttpClient的實例  
        HttpClient       httpClient   = new HttpClient();  
        HttpClientParams clientParams = new HttpClientParams();  
  
        // 隱藏自己請求相關的信息  
        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.159.58", 3128);  
  
        if (usePro)    // 使用代理  
        …{  
            httpClient.getHostConfiguration().setProxy(proIP, port);  
        }  
  
        // 創建GET方法的實例  
        GetMethod getMethod = new GetMethod("asp”>http://www.XXXcom/Guestbook/imgchk/validatecode.asp");  
  
        // 使用系統提供的默認的恢復策略  
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());  
  
        try …{  
  
            // 執行getMethod  
            int statusCode = httpClient.executeMethod(getMethod);  
  
            // System.out.println(statusCode);  
            if (statusCode != HttpStatus.SC_OK) {  
                System.err.println("Method failed: " + getMethod.getStatusLine());  
            }    // 讀取內容  
  
            InputStream inStream = getMethod.getResponseBodyAsStream();  
  
            // 處理內容  
            // 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.executeMethod(method);  
  
            // String Stringresponse = new String(method.getResponseBodyAsString().getBytes("8859_1"));  
            // 打印返回的信息  
            // System.out.println(Stringresponse);  
//          }  
  
            method.releaseConnection();  
  
//          System.out.println(iag.getHeight());  
//          System.out.println(iag.getWidth());  
//          //背景 顏色  
//          intBgColor = iag.getRGB(38, 0);  
//          System.out.println("intBgColor=" + intBgColor);  
//  
//  
//          intBgColor = iag.getRGB(0, 0);  
//          System.out.println("intBgColor=" + intBgColor);  
        } catch (Exception e) {  
  
            // 發生網絡異常  
            e.printStackTrace();  
        } finally {}  
  
        // 釋放連接   getMethod.releaseConnection();  }  
        getMethod.releaseConnection();  
    }  
}  
  

發佈留言