任何人都可以帮助将PHP代码转换为Java代码吗?我更喜欢使用Apache cxf webclient发送请求,但欢迎任何解决方案。
$services_url = 'http://example/services_wgea_drupal';
//(1) Server REST - user.login
// REST Server URL for auth
$request_url = $services_url . '/user/login';
// User data
$user_data = array(
'username' => 'aaaa',
'password' => 'bbbb',
);
$user_data = http_build_query($user_data);
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl);
这是我试过的:
WebClient client =
WebClient.create("http://example/services_wgea_drupal").accept(MediaType.APPLICATION_JSON);
client.path("/user/login").type(MediaType.APPLICATION_JSON);
Response response = client.post("{\"username\":\"aaaa\",\"password\":\"bbbb\"}");
响应代码是200但是根据我的PHP同事,应该返回包含会话名称和会话ID的json消息。但响应的responseMessage属性如下所示。我不认为这是一个正确的回应。
{org.apache.cxf.rest.message=true,
org.apache.cxf.invocation.context={ResponseContext={},
RequestContext={response.class=class javax.ws.rs.core.Response,
org.apache.cxf.request.method=POST,
org.apache.cxf.request.uri=http://example/services_wgea_drupal/user/login,
response.type=class javax.ws.rs.core.Response,
request.class=class java.lang.String,
jaxrs.proxy=false, org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[application/json],
Content-Type=[application/json]}, request.type=null,
org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://example/services_wgea_drupal/user/login}},
http.scheme=http, org.apache.cxf.client=true, org.apache.cxf.message.inbound=false,
org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[application/json], Content-Type= [application/json]},
org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://example/services_wgea_drupal/user/login,
org.apache.cxf.request.method=POST, org.apache.cxf.request.uri=http://example/services_wgea_drupal/user/login,
jaxrs.proxy=false, http.connection=sun.net.www.protocol.http.HttpURLConnection:http://example/services_wgea_drupal/user/login,
Content-Type=application/json, jaxrs.template.parameters=null,
org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.URLConnectionHTTPConduit737704879target: http://example.com/services_wgea_drupal/user/login, org.apache.cxf.message.Message.BASE_PATH=http://example.com/services_wgea_drupal}
答案 0 :(得分:0)
public static String sendPost(String url, String params) throws Exception {
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
// Act like a browser
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-Length", Integer.toString(params.length()));
connection.setDoOutput(true);
connection.setDoInput(true);
// Send post request
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "Windows-1251"));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
这种方法应该对你有帮助。 您可以像这样使用它:
sendPost("http://example/services_wgea_drupal/user/login", "username=aaaa&password=bbbb");