Java 如何进行POST请求并从服务器获得响应?
我需要通过以下示例向服务器发送POST请求: 请求格式:Java 如何进行POST请求并从服务器获得响应?,java,android,post,Java,Android,Post,我需要通过以下示例向服务器发送POST请求: 请求格式: POST /oauth/authorize HTTP/1.1 Host: m.sp-money.yandex.ru (для мобильных устройств) или sp-money.yandex.ru (для остальных устройств) Content-Type: application/x-www-form-urlencoded Content-Length: <content-length>
POST /oauth/authorize HTTP/1.1
Host: m.sp-money.yandex.ru (для мобильных устройств) или sp-money.yandex.ru (для остальных устройств)
Content-Type: application/x-www-form-urlencoded
Content-Length: <content-length>
client_id=<client_id>&response_type=code
&redirect_uri=<redirect_uri>&scope=<scope>
REQUEST PARAMETERS EXAMPLE:
client_id=092763469236489593523464667
response_type=code
redirect_uri=https://client.example.com/cb
scope=account-info operation-history
在你的后台,你可以试试这样的东西
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(HTTP_REQUEST_URL);
//create and assign post request server data
String latitude = loc.getLatitude() + "";
String longitude = loc.getLongitude() + "";
String time = DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) + "";
String whr = WhereAmI(loc.getLatitude(), loc.getLongitude());
//data back from server
String responseBackFromServer = "";
try {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("latitude", latitude));
pairs.add(new BasicNameValuePair("longitude", longitude));
pairs.add(new BasicNameValuePair("whereAmI", whr));
pairs.add(new BasicNameValuePair("time", time));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse server_response = client.execute(post);
responseBackFromServer = EntityUtils.toString(server_response.getEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return "Response Back: " + responseBackFromServer;
在onPostExecute中,您可以对响应执行任何操作。OK。我是否可以传输头等参数?post.addHeader一些名称,一些名称值;这应该行得通。
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=i1WsRn1uB1ehfbb37
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(HTTP_REQUEST_URL);
//create and assign post request server data
String latitude = loc.getLatitude() + "";
String longitude = loc.getLongitude() + "";
String time = DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) + "";
String whr = WhereAmI(loc.getLatitude(), loc.getLongitude());
//data back from server
String responseBackFromServer = "";
try {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("latitude", latitude));
pairs.add(new BasicNameValuePair("longitude", longitude));
pairs.add(new BasicNameValuePair("whereAmI", whr));
pairs.add(new BasicNameValuePair("time", time));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse server_response = client.execute(post);
responseBackFromServer = EntityUtils.toString(server_response.getEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return "Response Back: " + responseBackFromServer;