我正在使用Apache HttpClient生成Post請求並提交數據。 由於遠程應用程序中繼提交數據的用戶的IP地址,我想發送用戶指定IP地址的發佈請求。從Java中的特定IP生成HttpRequest
我該如何配置?
public static void loginUser(String username, String password, String ip) throws Exception{
try {
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://login.myapp.com/");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("username",username));
params.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// Execute and get the response.
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
//etc....
}
} catch (Exception e) {
e.printStackTrace();
}
}
編輯:爲了避免混淆,
我想包括在HttpRequest的頭自定義的IP地址,以便 終端應用的人都知道,這個請求[從我的應用程序]從定製 IP來了地址但不是來自我的應用程序運行的IP地址
假設我的應用程序在IP地址爲「1.1.1.0」的服務器上運行。 現在我的用戶正在執行帶有「test」,「test」,「199.199.199.0」的loginUser方法。 現在從應用程序到目的地URL的HTTP請求應該去爲它從「199.199.199.0」發送
你想獲得客戶端的IP? – smk
請說明你正在嘗試做什麼。 –
@JasonC我想包含自定義IP地址在httprequest頭,以便最終應用程序知道[從我的應用程序]這個請求來自自定義IP地址,但不是從我的應用程序正在運行的IP地址 – Reddy