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.