zoukankan      html  css  js  c++  java
  • 用Java模拟Http请求

    1),利用httpclient4.× 写一个http的客户端,模拟浏览器请求,
    public void post(List<NameValuePair> payload) throws  Exception{  
            HttpPost post = new HttpPost(uri);  
            HttpEntity result = null;  
            try {  
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(payload,  
                        charset);  
                post.setEntity(entity);  
                if (LOG.isDebugEnabled()) {  
                    LOG.debug("sending:" + payload);  
                }  
      
                HttpResponse response = _httpClient.execute(post);  
                StatusLine statusLine = response.getStatusLine();  
                if (statusLine.getStatusCode() != HttpStatus.SC_OK) {  
                    result = response.getEntity();  
                    StringBuilder msg = new StringBuilder();  
                    msg.append("http response with code "  
                            + statusLine.getStatusCode());  
                    msg.append("\n");  
                    msg.append("post request: " + post.getURI());  
                    msg.append("\n");  
                    msg.append(statusLine.getReasonPhrase());  
                    if (result != null) {  
                        msg.append("\n\n");  
                        msg.append(EntityUtils.toString(result, "UTF-8"));  
                        msg.append("\n\n");  
                    }  
                    throw new UmcException(msg.toString());  
                }  
                if (response.getEntity() != null) {  
                    BufferedReader reader = new BufferedReader(  
                            new InputStreamReader(  
                                    response.getEntity().getContent(), "UTF-8"));  
                    String line = null;  
                    while ((line = reader.readLine()) != null) {  
                        if (line.indexOf("success") < 0)  
                            System.out.println(line);  
                    }  
                }  
            } finally {  
                if (result != null)  
                    try {  
                        EntityUtils.consume(result);  
                    } catch (IOException e) {  
                    }  
                post.abort();  
            }  
        }  
    


    uri是请求的地址,charset是编码“UTF-8”,List<NameValuePair>就是表单参数集
    ClientConnectionManager     ccManager = new ThreadSafeClientConnManager();  
    HttpClient      _httpClient = new DefaultHttpClient(ccManager); 
    


    2) 采用JDK的HttpConnection构造http客户端,
    ////发送  
        HttpURLConnection conn = null;  
        try {  
            URL url = new URL(Your_URL);  
            conn = (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("POST");  
            conn.setRequestProperty("Content-Type",  
                    "application/x-www-form-urlencoded");  
            conn.setUseCaches(false);  
            conn.setDoOutput(true);  
            OutputStreamWriter osw = new OutputStreamWriter(  
                    conn.getOutputStream());  
            StringBuffer sb = new StringBuffer();  
            addPair(sb, "p1", "p1value");  
            addPair(sb, "p2", "p2value");  
            osw.write(sb.substring(0, sb.length() - 1));  
            osw.flush();  
            BufferedReader reader = new BufferedReader(  
                    new InputStreamReader(conn.getInputStream()));  
            String line = null;  
            sb = new StringBuffer();  
            while ((line = reader.readLine()) != null) {  
                sb.append(line);  
            }  
            line = sb.toString();  
            // 处理返回的字符串line  
            return;  
            ////  
        } catch (IOException e) {  
            // handle e  
        } finally {  
            if (conn != null)  
                conn.disconnect();  
        }///发送结束  
    

    addPair方法:
    public static void addPair(StringBuffer sb, String name, String value) {  
        if (value == null) {  
            return;  
        }  
        sb.append(name);  
        sb.append("=");  
        sb.append(value);  
        sb.append("&");  
    }  
    


  • 相关阅读:
    day04作业
    一个简单的gridlayout栗子
    用户名、密码等15个常用的js正则表达式
    html 颜色
    心态好的人,一辈子都好
    怎么样好好的聊天呢
    一篇引用文章
    再见,发微信不回的人
    第一个不怎么正经的网页
    关于学科目标
  • 原文地址:https://www.cnblogs.com/cwjcsu/p/8433094.html
Copyright © 2011-2022 走看看