Project

General

Profile

RE: 406 not acceptable on POST /issues.xml ยป FaultExample.java

Check Mate, 2012-05-31 10:10

 
1
package org.myredmine.test;
2

    
3
import java.io.InputStreamReader;
4
import java.io.OutputStreamWriter;
5
import java.net.URL;
6
import java.net.URLConnection;
7

    
8
public class FaultExample {
9

    
10
    private static String redmineHost = "http://192.168.37.62";
11
    private static String password    = "admin";
12
    private static String user        = "admin";
13

    
14
    public static void main(String[] args) {
15
        try {
16

    
17
            // build an xml string to post
18
            String request = "POST /issues.xml <issue> <subject>Example</subject> <project_id>1</project_id> <priority_id>1</priority_id> </issue>";
19

    
20
            // Prepare credentials
21
            String credentials = user + ":" + password;
22
            String encodedCredentials = "Basic " + new sun.misc.BASE64Encoder().encode(credentials.getBytes());
23

    
24
            // Open connection with authorization
25
            URL url = new URL(redmineHost + "/uploads.xml");
26
            URLConnection con = url.openConnection();
27
            con.setRequestProperty("Authorization", encodedCredentials);
28

    
29
            // specify that we will send output and accept input
30
            con.setDoInput(true);
31
            con.setDoOutput(true);
32

    
33
            // timeout specs
34
            con.setConnectTimeout(20000);
35
            con.setReadTimeout(20000);
36

    
37
            // disable cache
38
            con.setUseCaches(false);
39
            con.setDefaultUseCaches(false);
40

    
41
            // tell the web server what we are sending xml
42
            con.setRequestProperty("Content-Type", "application/xml");
43

    
44
            // write
45
            OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
46
            writer.write(request);
47
            writer.flush();
48
            writer.close();
49

    
50
            // reading response. That's were i get the 406.            
51
            InputStreamReader reader = new InputStreamReader(con.getInputStream());
52

    
53
            // stuff that's never reached
54
            StringBuilder buf = new StringBuilder();
55
            char[] cbuf = new char[2048];
56
            int num;
57

    
58
            while (-1 != (num = reader.read(cbuf))) {
59
                buf.append(cbuf, 0, num);
60
            }
61

    
62
            String result = buf.toString();
63
            System.err.println("\nResponse from server after POST:\n" + result);
64
        } catch (Exception e) {
65
            e.printStackTrace();
66
        }
67

    
68
        System.out.println("Ending ...");
69
    }
70
}
    (1-1/1)