خرید بک لینک

Vote count: 0

I am a newbie to REST API's. I need to post to the website, but I get response code as 400 and content-type as text/plain

If I use Advanced REST Client Application of google, I get different results. The response code is 500 and the content-type is text/html.

Am I not ending the post data (query1) correctly? Is this the correct way of doing it? Do I need to use JAX-RS? Can someone please help? Appreciate it.

import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLCoection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.testng.aotations.Test;

public class RestfullAPIHttpURLCoection {
        
        @Test
        public static void postS() throws Exception {
                
                URL url;
                HttpURLCoection coection = null;  
                
                String urlParameters  = "email=tester0@xxx.com&profileName=Tester0&password=test&roleId=1";
                
                String email = "tester0@xxx.com";
                String profileName = "Tester0";
                String password = "test";
                int roleId = 1;
                
                String query = String.format("email=%s&profileName=%s&password=%s&roleId=%s", 
                     (email), 
                     URLEncoder.encode(profileName),
                     URLEncoder.encode(password),
                     (roleId));
                String query1="?";
                query1 = query1.concat(query);
                System.out.println("query1: " +query1);
                
                String type = "application/json";
                
                url = new URL("http://......com");
                coection = (HttpURLCoection)url.openCoection();
                
                coection.setDoOutput(true);
                coection.setRequestMethod("POST");
                coection.setRequestProperty( "Content-Type", type );
                
                coection.setRequestProperty( "charset", "utf-8");
                coection.setUseCaches( false );
                
                // creates an output stream on the coection and opens an OutputStreamWriter on it:
                                
                        OutputStream output = coection.getOutputStream();
                        Writer writer = new OutputStreamWriter(output, "UTF-8");
                        
                        // client's output is the server's input.
                    writer.write(URLEncoder.encode(query1, "UTF-8"));
                
                
                String contentType=coection.getContentType();
                int responseCode=coection.getResponseCode();
                int len = coection.getContentLength();
                String rmsg = coection.getResponseMessage();
                
                System.out.println("ContentType: " +contentType);
                System.out.println("ResponseCode: " +responseCode);
                System.out.println("Content length: " +len);
                
                System.out.println("URL " + coection.getURL());
                System.out.println("Response msg: " + rmsg);
                
                
        }
        
}
asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 6 خرداد 1395 ساعت: 5:39

صفحه بندی