Question

I'm trying to post a comment from desktop application to Trac.

I'm using apache http client library in this project here is a link

Here is my code, sorry if it's hard to read

public class TestComment {

    private static String cookie;

    public static void main(String[] args) throws Exception {


        CookieHandler.setDefault(new CookieManager());
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://localhost:8080/mytrac/login");
        BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + encodedPassword("admin", "123123"));
        httpGet.addHeader(authHeader);
        HttpResponse response = defaultHttpClient.execute(httpGet);

        List<Cookie> cookies = defaultHttpClient.getCookieStore().getCookies();

        String token = null;
        if(!cookies.isEmpty()){
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
                token = cookies.get(i).toString().substring(43, 67);
                System.out.println(token);
            }
        }

        setCookie(token);

        responseLog(response);

        HttpPost httpPost = new HttpPost("http://localhost:8080/mytrac/ticket/2#comment:5");

        httpPost.setHeader(authHeader);

        httpPost.setHeader("Host", "localhost:8080");
        httpPost.setHeader("User-Agent", "Mozilla/5.0");
        httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpPost.setHeader("Accept-Language", "en-US,en;q=0.8");
        httpPost.setHeader("Connection", "keep-alive");
        httpPost.setHeader("Referer", "http://localhost:8080/mytrac/ticket/2");
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");


        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("__FORM_TOKEN", token));
        formparams.add(new BasicNameValuePair("comment", "Test comment"));
        formparams.add(new BasicNameValuePair("field_reporter", "admin"));



        httpPost.setEntity(new UrlEncodedFormEntity(formparams));

        response = defaultHttpClient.execute(httpPost);
        responseLog(response);

        System.out.println(response.getStatusLine());


    }

    private static String encodedPassword(String username, String password) {

        byte[] encodedPassword = (username + ":" + password).getBytes();
        BASE64Encoder base64Encoder = new BASE64Encoder();

        return base64Encoder.encode(encodedPassword);
    }

   private static void responseLog(org.apache.http.HttpResponse httpResponse) throws IOException {

       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse. getEntity().getContent()));

       StringBuffer stringBuffer = new StringBuffer();
       String line1;

       while ((line1 = bufferedReader.readLine()) != null) {
           stringBuffer.append(line1 + "\n");
       }

       System.out.println(stringBuffer) ;
   }

    public static String getCookie() {
        cookie = cookie.substring(cookie.indexOf(":") + 1);
        return cookie;
    }

    public static void setCookie(String cookie) {
        TestComment.cookie = cookie;
    }
}

When I run this code I get 200 code it tells ok and I even get my comment in Text-Aria form, but don't post it. When I post a comment in browser the code is 303. Where am I wrong, may be I am on totaly wrong way?

Was it helpful?

Solution

We resolved the problem

I didn't know but i just needed to send one more form, we need to get view time from trac and send it as a form:

formparams.add(new BasicNameValuePair("view_time", view_time));

now it works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top