简体   繁体   中英

Post request with payload

I have to send post request with parameters:

"Query String Parameters" : variant=1

"Request payload": {"operationDate":"2014-07-09T00:00:00.000Z"}

I saw examples for "Query String Parameters"

List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("variant", "1"));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
httpost.setEntity(urlEncodedFormEntity);

And for "Request payload"

String payload = "{\"operationDate\":\"2014-07-09T00:00:00.000Z\"}";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);

How could i set both parameters to request?

If im doing like this

httpclient = new DefaultHttpClient();
httpost = new HttpPost("https://energy.so-ups.ru/PrimaryInput.aspx/GetData?variant=1");

nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("variant", "1"));
nameValuePairs.add(new BasicNameValuePair("operationDate", "2014-07-09T00:00:00.000Z"));

httpost.setHeader("Host", "energy.so-ups.ru");
httpost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
httpost.setHeader("Accept", "*/*");
httpost.setHeader("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
httpost.setHeader("Accept-Encoding", "gzip, deflate");
httpost.setHeader("Content-Type", "application/json; charset=UTF-8");
httpost.setHeader("X-Requested-With", "XMLHttpRequest");
httpost.setHeader("Referer", "https://energy.so-ups.ru/PrimaryInput.aspx?variant=1");
httpost.setHeader("Cookie", cookie);
httpost.setHeader("Connection", "keep-alive");
httpost.setHeader("Keep-Alive", "header");
httpost.setHeader("Pragma", "no-cache");
httpost.setHeader("Cache-Control", "no-cache");

urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);

httpost.setEntity(urlEncodedFormEntity);

response = httpclient.execute(httpost);

Im getting status code 500

Your payload is not in a format for the request object to know. The correct format is operationDate=2014-07-09T00:00:00.000Z

The format is the same as that for the ones included in the URL itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM