我必须发送带有参数的帖子请求:
“查询字符串参数”:variant = 1
“请求有效负载”:{“operationDate”:“2014-07-09T00:00:00.000Z”}
我看到了“查询字符串参数”
的示例List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("variant", "1"));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
httpost.setEntity(urlEncodedFormEntity);
对于“请求有效载荷”
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);
如何设置两个参数以供请求?
如果我这样做
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);
我获取状态代码500
答案 0 :(得分:0)
您的有效内容不是请求对象要知道的格式。正确的格式是operationDate = 2014-07-09T00:00:00.000Z
格式与网址中包含的格式相同。