通过org.apache.http.client.methods.HttpPost
发布表单数据的正确方法是什么?
我的邮递员方案看起来像这样(并且运作正常):
我需要使用HttpPost
将数据传递给服务器。
我尝试使用值分析:
HttpPost post = new HttpPost(url);
post.addHeader("cache-control", "no-cache");
post.addHeader("content-type", "multipart/form-data");
List< NameValuePair > nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("token", "Bearer " + token));
nameValuePairs.add(new BasicNameValuePair("city", address.getCity()));
< ... and ect. >
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
但是我收到了来自服务器的错误消息。
我还尝试使用postman为OKHTTP
生成的字符串:
post.addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
post.setEntity(new StringEntity("-----011000010111000001101001\r\nContent-Disposition: form-data; name="token"\r\n\r\nBearer 7ts7m6qoi67qtf619xreb8haqjaa5lax\r\-----011000010111000001101001\r\nContent-Disposition: form-data; name="city"\r\n\r\ncity\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; <ect.> "
但得到了相同的结果