我有这个,
@POST
@Path("/login")
public Response postLogin() throws IOException, SQLException,NamingException {
return Response.temporaryRedirect(URI.create("localdir")).build();
}
它返回POST。但是我想获得相同帖子的GET方法。
我该怎么做?
答案 0 :(得分:0)
试试这个,
@POST
@Path("/login")
public Response postLogin() throws IOException, SQLException,NamingException {
URL obj;
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response;
}
或者,
@POST
@Path("/login")
public Response postLogin() throws IOException, SQLException,NamingException {
Request.Get("your url").execute().returnContent();
}
或者,
@POST
@Path("/login")
public Response postLogin() throws IOException, SQLException,NamingException {
String uri = "your url";
HttpParams httpParams = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet(uri);
HttpResponse response = client.execute(request);
String responseStr = buildResponseFromEntity(response.getEntity());
private String buildResponseFromEntity(HttpEntity entity)throws IllegalStateException, IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
return total.toString();
}
// check if error
if (response.getStatusLine().getStatusCode() != 200) {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(responseStr);
} catch (Exception e) {
// do your code
}
}
}
答案 1 :(得分:0)
尽管答案是对的,并且因为我将它用于HTML,所以这是一种简单的黑客方法。
String s = "<script>window.location.href="%s"</script>";
s = String.format(s, "localdir");
return Response.status(Response.Status.OK)
.entity(s)
.build();