Bonjour,

J'ai une appli écrite en C# (Compact framework 2.0). Elle concatène des paramètres dans l'URL de cette façon (un envoi de fichier est fait plus loin dans le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
string url = "http://192.168.20.74:8080/SynchroServer_4/servlet/monservlet");
 
string param = "";
string mimetype = "application/gzip";
int isize = 0;
HttpWebRequest request = null;
Stream SendStream = null;
byte []receivedbuffer = new byte[512];
 
param += "&" + Globals.COMMENT_PARAMETER + "=" + HttpUtility.UrlEncode(m_comment);
 
url += "?" + param;
request = HttpTransmission.CreateRequest(url, mimetype, isize, "POST");
SendStream = request.GetRequestStream();
La méthode HTTPTransmission.CreateRequest :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
 public static HttpWebRequest CreateRequest(string url, string contenttype, int contentlength, string method)
{
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.ContentLength = contentlength;
            req.ContentType = contenttype;
            req.Method = method;
            req.AllowAutoRedirect = true;
            req.Proxy = new WebProxy();
            req.Timeout = TIMEOUT;
            return (req);
}
La classe HttpUtility :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
102
103
104
105
106
107
108
 
 
private static Encoding inputEncoding = Encoding.Default;
private static Encoding outputEncoding = Encoding.UTF8;
 
public static string UrlEncode(string input)
{
            string result = null;
            byte[] buffer = null;
            byte[] input = null;
 
            if (input_string != null)
            {
                input = inputEncoding.GetBytes(input_string);
                if (input != null)
                {
                    buffer = UrlEncodeToBytes(input, 0, input.Length);
                    if (buffer != null)
                    {
                        result = outputEncoding.GetString(buffer, 0, buffer.Length);
                    }
                }
            }
 
            return (result);
}
 
 /// <summary>
        /// Encode the input and protects needed characters
        /// return the result to a byte array
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count)
        {
            byte[] result = null;
            List<byte> buffer = null;
            int lastIndex = 0;
            int i = 0;
            int index = 0;
            char current_char = '\0';
 
            if (bytes != null && bytes.Length > 0)
            {
                if (offset < 0 || offset >= bytes.Length)
                { throw new ArgumentOutOfRangeException("offset"); }
 
                if (count < 0 || offset < (bytes.Length - count))
                { throw new ArgumentOutOfRangeException("count"); }
 
                buffer = new List<byte>();
 
                for (i = offset, lastIndex = offset + count; i < lastIndex; i++)
                {
                    current_char = Convert.ToChar(bytes[i]);
 
                    //According to the documentation I've found, we only need to encode characters when:
                    //  --> the character is a control char
                    //  --> the character is a non-ascii character (byte value is between [0x80 and 0xFF])
                    //  --> the character is a white space
                    //  --> the character is a reserved char or unsafe char
                    if (char.IsControl(current_char)
                            ||
                        (current_char >= 0x80 && current_char <= 0xFF)
                            ||
                        char.IsWhiteSpace(current_char)
                            ||
                        (System.Array.IndexOf<char>(g_ReservedCharacters, current_char) != -1))
                    {
                        if (current_char == ' ')
                        {
                            //Special case to for space char : we transform it as a plus
                            //Allow us to generate smaller url than using the hexadecimal notation '%20'
                            buffer.Add(Convert.ToByte('+'));
                        }
                        else
                        {
                            //We add the hexadecimal value
                            //We take the 4 highest bits to retrieve its hexa value 
                            //For example : if current_char == '+'
                            //Its integer value is 0x0000002B
                            //and its integer value applied to operator >> 4 equals 2. So we take the character which is 
                            //at index 2 in the g_HexadecimalCharacters array (for us : '2').
                            //Then we apply the mask 0x0000000F on the integer value of the char : 0x000000B
                            //Finally we take the character which is at index 0xB (== 11 dec) in the g_HexadecimalCharacters array (for us : 'B')
                            index = Convert.ToInt32(current_char) >> 4; //We process the high order bits...
                            buffer.Add(Convert.ToByte('%')); buffer.Add(Convert.ToByte(g_HexadecimalCharacters[index]));
                            index = Convert.ToInt32(current_char) & 0x0000000F; //We keep the last 4 bits
                            buffer.Add(Convert.ToByte(g_HexadecimalCharacters[index]));
                        }
                    }
                    else
                    {
                        //We simply add the value...
                        buffer.Add(Convert.ToByte(current_char));
                    }
                }
 
                if (buffer.Count > 0)
                {
                    result = buffer.ToArray();
                }
            }
 
            return (result);
        }


Coté Tomcat :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		processRequest(arg0,arg1);
	}
 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException
	{
 
        request.setCharacterEncoding("UTF8");
 
	string Comment = request.getParameter(Globals.COMMENT_PARAMETER);
 
 
 
System.out.println("Setting comments : " + Comments);
J'obtiens (les caractères étranges étants des "é" et "è")
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
Setting comments to order 110563867 : xghjuhuffdèéèéèéèéè
Merci d'avance !