Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I call HTTP POST in plug-in development

harainye
Contributor
December 14, 2020

hi,I've been working on plug-in development recently.I tried to invoke another service in the REST interface,But I never succeeded.

Here is part of my code:

@POST
@AnonymousAllowed
@Path("/turnToNext/{title}")
@Produces({MediaType.APPLICATION_JSON})
public Response turnToNext(@PathParam("title") String title) throws IOException {
......
(here I need to call HTTP post to another service)
......
return Response.ok(JSONUtils.toJSONObject(result)).build();
}

 I checked the following HTTP method on the Internet, and the main method was successfully called, but the call in the plug-in did not work.

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.util.*;

public class HttpClientUtil {
static CloseableHttpClient client = null;

static {
client = HttpClients.createDefault();
}


public static String get(String url, HashMap<String, Object> params) throws Exception {
HttpGet httpGet = new HttpGet();
Set<String> keySet = params.keySet();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(url).append("?t=").append(System.currentTimeMillis());
for (String key : keySet) {
stringBuffer.append("&").append(key).append("=").append(params.get(key));
}
httpGet.setURI(new URI(stringBuffer.toString()));
CloseableHttpResponse execute = client.execute(httpGet);
int statusCode = execute.getStatusLine().getStatusCode();
if (200 != statusCode) {
return "";
}
return EntityUtils.toString(execute.getEntity(), "utf-8");
}

public static String get2(String url, String params) throws Exception {
HttpGet httpGet = new HttpGet();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(url).append("/").append(params);
httpGet.setURI(new URI(stringBuffer.toString()));
CloseableHttpResponse execute = client.execute(httpGet);
int statusCode = execute.getStatusLine().getStatusCode();
if (200 != statusCode) {
return "";
}
return EntityUtils.toString(execute.getEntity(), "utf-8");
}


public static String post(String url, HashMap<String, Object> params) throws Exception {
HttpPost httpPost = new HttpPost();
httpPost.setURI(new URI(url));
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
for (String key : keySet) {
NameValuePair e = new BasicNameValuePair(key, params.get(key).toString());
parameters.add(e);
}
HttpEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
httpPost.setEntity(entity);
CloseableHttpResponse execute = client.execute(httpPost);
int statusCode = execute.getStatusLine().getStatusCode();
if (200 != statusCode) {
return "";
}
return EntityUtils.toString(execute.getEntity(), "utf-8");
}


public static String post(String url, HashMap<String, Object> params, HashMap<String, Object> headers) throws Exception {
HttpPost httpPost = new HttpPost();
Set<String> keySet2 = headers.keySet();
Iterator<String> iterator = keySet2.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = headers.get(key).toString();
httpPost.addHeader(key, value);
}

httpPost.setURI(new URI(url));
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
for (String key : keySet) {
NameValuePair e = new BasicNameValuePair(key, params.get(key).toString());
parameters.add(e);
}
HttpEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
httpPost.setEntity(entity);
CloseableHttpResponse execute = client.execute(httpPost);
int statusCode = execute.getStatusLine().getStatusCode();
if (200 != statusCode) {
return "";
}
return EntityUtils.toString(execute.getEntity(), "utf-8");
}


public static String postJson(String url, HashMap<String, Object> params) throws Exception {
HttpPost httpPost = new HttpPost();
httpPost.setURI(new URI(url));
String jsonString = JSON.toJSONString(params);
StringEntity stringEntity = new StringEntity(jsonString, "utf-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
CloseableHttpResponse execute = client.execute(httpPost);
int statusCode = execute.getStatusLine().getStatusCode();
if (200 != statusCode) {
return "";
}
return EntityUtils.toString(execute.getEntity(), "utf-8");
}

}   

 It would be very kind of anyone to help me with this problem.

Thank you!

1 answer

1 accepted

0 votes
Answer accepted
harainye
Contributor
December 15, 2020

I've solved it.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events