admin
2020-09-05 d3ebf5b103d4deebd6ffb75f4471a6fddab8d764
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.qcloud.cmq;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
 
public class CMQHttp {
    private  int timeout ;
    private  boolean isKeepAlive;
    private URLConnection connection;
    private String url ;    
 
    public CMQHttp()
    {
        this.connection = null;
        this.url = "";
        this.timeout = 10000;
        this.isKeepAlive = true;
    }
    /*
     * if we find the url is different with this.url we should new another connection 
     * 
     */
    private void newHttpConnection(String url) throws Exception
    {
        if(this.url != url)
        {
            URL realUrl = new URL(url);
            if(url.toLowerCase().startsWith("https")){
                HttpsURLConnection httpsConn = (HttpsURLConnection)realUrl.openConnection();
                httpsConn.setHostnameVerifier(new HostnameVerifier(){
                    public boolean verify(String hostname, SSLSession session){
                        return true;
                    }
                });
                connection = httpsConn;
            }
            else{
                connection = realUrl.openConnection();
            }
               this.connection.setRequestProperty("Accept", "*/*");
            if(this.isKeepAlive)
                this.connection.setRequestProperty("Connection", "Keep-Alive");
            this.connection.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 
            this.url = url ;
        }
    }
    public  String request(String method, String url, String req,
            int userTimeout) throws Exception {
        String result = "";
        BufferedReader in = null;
        try{
            if (this.url != url)
                this.newHttpConnection(url);
 
            this.connection.setConnectTimeout(timeout+userTimeout);
    
            if (method.equals("POST")) {
                ((HttpURLConnection)this.connection).setRequestMethod("POST");
    
                this.connection.setDoOutput(true);
                this.connection.setDoInput(true);
                DataOutputStream out = new DataOutputStream(this.connection.getOutputStream());
                out.writeBytes(req);
                out.flush();
                out.close();
            }
 
            this.connection.connect();
            int status = ((HttpURLConnection)this.connection).getResponseCode();
            if(status != 200)
                throw new CMQServerException(status);
    
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
    
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        }catch(Exception e){
            throw e;
        }finally{
            try {
                if (in != null) 
                    in.close();
            } catch (Exception e2) {
                throw e2;
            }
        }
        
        return result;
    }
}