java推送資料,java代理post提交資料
阿新 • • 發佈:2019-02-07
java推送資料,代理post提交資料
網上看到的。。 留下備用。
[java] view plaincopyprint?- 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;
- publicclass MyHttpClient {
- /** *//**
- * Method description
- *
- *
- * @param title 留言標題
- * @param name 留言者
- * @param Content 內容
- * @param proIP 代理IP
- * @param port 代理埠
- * @param usePro 是否使用代理
- */
- publicsynchronizedvoid doSomeThing(String title, String name, String Content, String proIP, int port,
- boolean usePro) {
- // 構造HttpClient的例項
- HttpClient httpClient = new HttpClient();
- HttpClientParams clientParams =
new
- // 隱藏自己請求相關的資訊
- 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("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();
- }
- }