<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,comments - 227,trackbacks - 0
    package com.test;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    import org.apache.commons.httpclient.Cookie;
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.URI;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.cookie.CookieSpec;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;

    public class HttpWeb {

        
    public static String getGetResponse(String url) {
            String html 
    = "";
            
    // 構(gòu)造HttpClient的實例
            HttpClient httpClient = new HttpClient();
            
    // 創(chuàng)建GET方法的實例
            GetMethod getMethod = new GetMethod(url);
            
    // 使用系統(tǒng)提供的默認(rèn)的恢復(fù)策略
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            
    try {
                
    // 執(zhí)行g(shù)etMethod
                int statusCode = httpClient.executeMethod(getMethod);
                
    if (statusCode != HttpStatus.SC_OK) {
                    System.err.println(
    "Method failed: " + getMethod.getStatusLine());
                }
                
    // 處理內(nèi)容
                html = getMethod.getResponseBodyAsString();
            } 
    catch (HttpException e) {
                
    // 發(fā)生致命的異常,可能是協(xié)議不對或者返回的內(nèi)容有問題
                System.out.println("Please check your provided http address!");
                e.printStackTrace();
            } 
    catch (IOException e) {
                
    // 發(fā)生網(wǎng)絡(luò)異常
                e.printStackTrace();
            } 
    finally {
                
    // 釋放連接
                getMethod.releaseConnection();
            }
            
    return html;
        }

        
    public static String getPostResponse(String url) throws HttpException, IOException {
            String html 
    = "";
            HttpClient httpClient 
    = new HttpClient();
            PostMethod postMethod 
    = new PostMethod(url);
            postMethod.setRequestHeader(
    "accept""*/*");
            postMethod.setRequestHeader(
    "connection""Keep-Alive");
            postMethod.setRequestHeader(
    "user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            postMethod.setRequestHeader(
    "Accept-Language""zh-cn,zh;q=0.5");
            
    // postMethod.setRequestHeader("Accept-Encoding", "gzip,deflate");
            
    // postMethod.setRequestHeader("Content-Type", "text/html;charset=utf-8");
            
    // 填入各個表單域的值
            NameValuePair[] data = { new NameValuePair("msg""你好") };
            
    // 將表單的值放入postMethod中
            postMethod.setRequestBody(data);
            
    // 執(zhí)行postMethod
            int statusCode = httpClient.executeMethod(postMethod);
            
    // HttpClient對于要求接受后繼服務(wù)的請求,象POST和PUT等不能自動處理轉(zhuǎn)發(fā)
            
    // 301或者302
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                
    // 從頭中取出轉(zhuǎn)向的地址
                Header locationHeader = postMethod.getResponseHeader("location");
                String location 
    = null;
                
    if (locationHeader != null) {
                    location 
    = locationHeader.getValue();
                    System.out.println(
    "The page was redirected to:" + location);
                } 
    else {
                    System.err.println(
    "Location field value is null.");
                }
                
    return html;
            }
            
    // html = postMethod.getResponseBodyAsString();

            System.out.println(postMethod.getResponseCharSet());
            
    // byte[] responseBody = postMethod.getResponseBody();

            BufferedReader in 
    = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), postMethod
                    .getResponseCharSet()));
            StringBuffer sb 
    = new StringBuffer();
            
    int chari;
            
    while ((chari = in.read()) != -1) {
                sb.append((
    char) chari);
            }
            html 
    = sb.toString();
            in.close();
            postMethod.releaseConnection();

            CookieSpec cookiespec 
    = CookiePolicy.getDefaultSpec();
            postMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
            URI uri 
    = postMethod.getURI();
            Cookie[] cookies 
    = cookiespec.match(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort(), "/"false,
                    httpClient.getState().getCookies());
            
    for (Cookie cookie : cookies) {
                System.out.println(cookie.getName());
                System.out.println(cookie.getValue());
            }

            postMethod.setURI(
    new URI("http://www.ssread.com/zt/writecookie2.jsp"false));
            httpClient.executeMethod(postMethod);
            BufferedReader in2 
    = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), postMethod
                    .getResponseCharSet()));
            StringBuffer sb2 
    = new StringBuffer();
            
    int chari2;
            
    while ((chari2 = in2.read()) != -1) {
                sb2.append((
    char) chari2);
            }
            html 
    = sb2.toString();
            in2.close();
            postMethod.releaseConnection();
            
    // PostMethod postMethod2 = new PostMethod("http://www.ssread.com/zt/writecookie2.jsp");
            
    // // httpClient.getState().addCookies(cookies);
            
    // httpClient.executeMethod(postMethod2);
            
    // BufferedReader in2 = new BufferedReader(new InputStreamReader(postMethod2.getResponseBodyAsStream(),
            
    // postMethod2.getResponseCharSet()));
            
    // StringBuffer sb2 = new StringBuffer();
            
    // int chari2;
            
    // while ((chari2 = in2.read()) != -1) {
            
    // sb2.append((char) chari2);
            
    // }
            
    // html = sb2.toString();
            
    // in2.close();
            
    // postMethod2.releaseConnection();
            return html;
        }

        
    public static class UTF8PostMethod extends PostMethod {
            
    public UTF8PostMethod(String url) {
                
    super(url);
            }

            @Override
            
    public String getRequestCharSet() {
                
    return super.getRequestCharSet();
                
    // return "UTF-8";
            }
        }

        
    public static void main(String[] args) throws HttpException, IOException {
            System.out.println(HttpWeb.getPostResponse(
    "http://www.ssread.com/zt/writecookie.jsp"));
        }
    }
    posted on 2009-06-25 17:52 SIMONE 閱讀(12940) 評論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 一级做a爱过程免费视| 国产精品免费一区二区三区| 日韩一级免费视频| 免费人成网站永久| 亚洲国产精品乱码一区二区 | 免费国产在线精品一区| 亚洲中文字幕日产乱码高清app| a级毛片无码免费真人久久| 18gay台湾男同亚洲男同| 成年人免费视频观看| 国产精品亚洲一区二区三区在线观看 | 日韩欧美亚洲中文乱码| 亚洲人色婷婷成人网站在线观看| 最近免费中文字幕大全高清大全1 最近免费中文字幕mv在线电影 | 亚洲AV日韩AV永久无码免下载| 一个人免费观看www视频在线| 黄色网页免费观看| 亚洲综合在线观看视频| 四虎永久免费网站免费观看| 日韩精品久久久久久免费| 色婷婷亚洲一区二区三区| 亚洲天天做日日做天天看| 亚洲精品国产精品国自产观看| 久久免费看黄a级毛片 | 日韩免费一级毛片| 欧洲精品99毛片免费高清观看| 西西人体大胆免费视频| 亚洲午夜国产精品无卡| 亚洲永久精品ww47| 国产在线观看免费完整版中文版 | 精品亚洲成在人线AV无码| 亚洲色大成网站www永久一区| 免费无码黄网站在线观看| 亚洲人成在线免费观看| 久久久久久噜噜精品免费直播| 亚洲日本VA中文字幕久久道具| 久久久久亚洲AV无码网站| 国产亚洲精AA在线观看SEE| 亚洲av日韩av欧v在线天堂| 天天摸夜夜摸成人免费视频| 久久午夜伦鲁片免费无码|