来自 Post 人的 HTTP Post 工作但不是通过浏览器
HTTP Post from Postman working but not through browser
我在服务器上使用 JaxRS jersey,我已经将它部署在 aws 上。对服务器的 Http post 请求与 postman 一起工作,但与 Http post apache 客户端一起工作。
以下是我的 Java 休息服务
@Path("/data")
public class MyResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<trackerdetails> getIt() {
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
trackerdetails user = new trackerdetails();
List<trackerdetails> sendlist = (List<trackerdetails>) session.createQuery("from trackerdetails").list();
session.getTransaction().commit();
session.close();
return sendlist;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public trackerdetails putit(trackerdetails track) {
track.setDate(new Date());
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
session.save(track);
session.getTransaction().commit();
session.close();
return track;
}
以下是我的追踪器详情class
@Entity
@XmlRootElement
public class trackerdetails {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int autoid;
private String latitude;
private String longitude;
private String devicename;
private Date date;
public trackerdetails(){
}
public int getAutoid() {
return autoid;
}
public void setAutoid(int autoid) {
this.autoid = autoid;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getDevicename() {
return devicename;
}
public void setDevicename(String devicename) {
this.devicename = devicename;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
以下是我的客户端 httppost 请求
HttpPost httpPost = new HttpPost("myurl");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("devicename", "vip"));
nvps.add(new BasicNameValuePair("date", "hjksvn"));
nvps.add(new BasicNameValuePair("latitude", "hello"));
nvps.add(new BasicNameValuePair("longitude","hi"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Host", "trackertest.herokuapp.com");
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
System.out.println(response2.toString());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
BufferedReader rd = new BufferedReader(
new InputStreamReader(response2.getEntity().getContent()));
StringBuffer result1 = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result1.append(line);
System.out.println(line);
System.out.println("");
}
System.out.println(result1);
EntityUtils.consume(entity2);
} finally {
response2.close();
}
以下是我的错误状态是400
Bad Request
描述 客户端发送的请求在语法上不正确。
您的 REST API 需要 JSON
格式的请求,但您使用 NameValuePair
构建请求正文的方式不会导致 JSON
格式。
您应该通过使用一些可以将对象转换为 JSON
的库来制作有效的 JSON
请求正文,例如 Jackson
或者您可以手动构建 JSON
请求正文然后打电话给你的 API.
下面是手动构造JSON
请求体的一种方式-
HttpPost httpPost = new HttpPost("myurl");
StringBuilder jsonBody = new StringBuilder();
jsonBody.append("{");
jsonBody.append("\"devicename\" : ").append("\"vip\"").append(",");
// Pass a valid date because on server side, you are using Date object for accepting it
jsonBody.append("\"date\" : ").append("\"2017-09-23\"").append(",");
jsonBody.append("\"latitude\" : ").append("\"vip\"").append(",");
jsonBody.append("\"longitude\" : ").append("\"vip\"");
jsonBody.append("}");
StringRequestEntity requestEntity = new StringRequestEntity(jsonBody.toString(),"application/json","UTF-8");
httpPost.setRequestEntity(requestEntity);
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Host", "trackertest.herokuapp.com");
// Rest code should remain same
我在服务器上使用 JaxRS jersey,我已经将它部署在 aws 上。对服务器的 Http post 请求与 postman 一起工作,但与 Http post apache 客户端一起工作。 以下是我的 Java 休息服务
@Path("/data")
public class MyResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<trackerdetails> getIt() {
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
trackerdetails user = new trackerdetails();
List<trackerdetails> sendlist = (List<trackerdetails>) session.createQuery("from trackerdetails").list();
session.getTransaction().commit();
session.close();
return sendlist;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public trackerdetails putit(trackerdetails track) {
track.setDate(new Date());
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
session.save(track);
session.getTransaction().commit();
session.close();
return track;
}
以下是我的追踪器详情class
@Entity
@XmlRootElement
public class trackerdetails {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int autoid;
private String latitude;
private String longitude;
private String devicename;
private Date date;
public trackerdetails(){
}
public int getAutoid() {
return autoid;
}
public void setAutoid(int autoid) {
this.autoid = autoid;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getDevicename() {
return devicename;
}
public void setDevicename(String devicename) {
this.devicename = devicename;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
以下是我的客户端 httppost 请求
HttpPost httpPost = new HttpPost("myurl");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("devicename", "vip"));
nvps.add(new BasicNameValuePair("date", "hjksvn"));
nvps.add(new BasicNameValuePair("latitude", "hello"));
nvps.add(new BasicNameValuePair("longitude","hi"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Host", "trackertest.herokuapp.com");
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
System.out.println(response2.toString());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
BufferedReader rd = new BufferedReader(
new InputStreamReader(response2.getEntity().getContent()));
StringBuffer result1 = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result1.append(line);
System.out.println(line);
System.out.println("");
}
System.out.println(result1);
EntityUtils.consume(entity2);
} finally {
response2.close();
}
以下是我的错误状态是400 Bad Request
描述 客户端发送的请求在语法上不正确。
您的 REST API 需要 JSON
格式的请求,但您使用 NameValuePair
构建请求正文的方式不会导致 JSON
格式。
您应该通过使用一些可以将对象转换为 JSON
的库来制作有效的 JSON
请求正文,例如 Jackson
或者您可以手动构建 JSON
请求正文然后打电话给你的 API.
下面是手动构造JSON
请求体的一种方式-
HttpPost httpPost = new HttpPost("myurl");
StringBuilder jsonBody = new StringBuilder();
jsonBody.append("{");
jsonBody.append("\"devicename\" : ").append("\"vip\"").append(",");
// Pass a valid date because on server side, you are using Date object for accepting it
jsonBody.append("\"date\" : ").append("\"2017-09-23\"").append(",");
jsonBody.append("\"latitude\" : ").append("\"vip\"").append(",");
jsonBody.append("\"longitude\" : ").append("\"vip\"");
jsonBody.append("}");
StringRequestEntity requestEntity = new StringRequestEntity(jsonBody.toString(),"application/json","UTF-8");
httpPost.setRequestEntity(requestEntity);
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Host", "trackertest.herokuapp.com");
// Rest code should remain same