0

So I'm trying to post to a URL (which I can't show, because it's sensitive information), but I keep getting a 401 error, which means unauthorized. And I have credentials, but I don't know how to set them. I have a username, password, and a role.

public class GSONPost {

public static void main(String[] args) {

    Person personObj = new Person();
    personObj.setOrganization("ACC");
    personObj.setFirstName("Harry");
    personObj.setLastName("Smith");
    personObj.setPhone1(null);
    personObj.setPhone2(null);
    personObj.setEmail(null);
    personObj.setState("AZ");
    personObj.setLeadDate(null); // Fix Later
    personObj.setCompany("Dan's Mortage");
    personObj.setCompanyContactName("Indiana Jones");
    personObj.setOutsideRep("Joel Martin");

    Person personObj2 = new Person();
    personObj2.setOrganization("ACC");
    personObj2.setFirstName("Richard");
    personObj2.setLastName("Nixon");
    personObj2.setPhone1(null);
    personObj2.setPhone2(null);
    personObj2.setEmail(null);
    personObj2.setState(null);
    personObj2.setLeadDate(null); // Fix Later
    personObj2.setCompany("Dan's Mortage");
    personObj2.setCompanyContactName("Indiana Jones");
    personObj2.setOutsideRep("Joel Martin");

    List<Person> personArrayList = new ArrayList<Person>();
    personArrayList.add(personObj);
    personArrayList.add(personObj2);

    PersonList personList = new PersonList();
    personList.setPersonList(personArrayList);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    String json = gson.toJson(personList);

    try {
        // write converted json data to a file named "PersonList.json"
        FileWriter writer = new FileWriter("C:\\Users\\Dylan\\JsonFiles\\PersonList.json");
        writer.write(json);
        writer.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    try

    {


        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(
                "https://myurl/addlist");

        StringEntity input = new StringEntity(json);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        httpClient.getConnectionManager().shutdown();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e1) {

        e1.printStackTrace();

    }
}
}

If anyone can show me a way where I could set my credentials, so that I won't get a 401 error, or if you guys can see anything else that's causing the issue and could let me know, that would be great. Thanks in advance.

6
  • What kind of authentication the service expecting? Commented Mar 8, 2016 at 4:52
  • I'm not sure, how can I tell? This is my first project like this. Commented Mar 8, 2016 at 4:53
  • All the error says is reason unauthorized Commented Mar 8, 2016 at 4:54
  • 1
    @dochsner, add authentication information to request object. You could see this stackoverflow.com/questions/3283234/… Commented Mar 8, 2016 at 4:57
  • You need to configure your SSL settings. In simple terms, either your code or the server is refusing to proceed because of security settings. Commented Mar 8, 2016 at 5:01

1 Answer 1

1

You need to configure your SSL settings before you make the call. Currently, your POST is failing most likely during the SSL handshake. A quick fix would be to trust all certificates. Here is a code snippet which will do just that:

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

HttpPost postRequest = new HttpPost("https://myurl/addlist");

StringEntity input = new StringEntity(json);
input.setContentType("application/json");
postRequest.setEntity(input);

HttpResponse response = httpClient.execute(postRequest);
Sign up to request clarification or add additional context in comments.

17 Comments

What's the new error?
When I try to put this code in the Eclipse it underlines this line in red builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); and says Unhandled exception type KeyStoreException
Huh? Are you missing a dependency or something? You will have to resolve this on your own, I believe my strategy is correct for your problem.
I'm using httpclient-4.3.1.jar
Ok, I may have got it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.