[英]how to pass arguments to GET method in RESTfull webservice
我必须将UserName
和password
作为参数传递给GET
方法进行验证。处理后我需要得到响应。那么我如何将值传递给RESTful
webservice GET
方法?
Arguments are generally passed in a GET request as URL parameters. 参数通常作为URL参数在GET请求中传递。 Without knowing what you've tried or where you're struggling, I can't really provide much more of an answer than that.
如果不知道你曾经尝试过什么或者你在哪里挣扎,我真的无法提供更多的答案。
--UPDATE-- --UPDATE--
Try this: http://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/ 试试这个: http : //www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/
To pass parameters in HTTP GET you should use a ?
要在 HTTP GET 中传递参数,您应该使用
?
delimiter.分隔符。 Such as
如
https://mywebsite.com/user/login?username=bob&password=123
https://mywebsite.com/user/login?paramname1=value1¶mname2=value2
Make sure to always use https
with any sensitive data.确保始终对任何敏感数据使用
https
。 You may also need to escape/encode both username and password to allow extended ASCII.您可能还需要对用户名和密码进行转义/编码以允许扩展 ASCII。 If you need to support UNICODE you should consider using a POST request.
如果您需要支持 UNICODE,您应该考虑使用 POST 请求。
You can do the following for Login validation,您可以执行以下登录验证,
// Create a new HttpClient and Post Header
String downloadedString= null;
HttpClient httpclient = new DefaultHttpClient();
//for registerhttps://te
HttpPost httppost = new HttpPost("YOUR LOGIN URL");
//add data
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", UserName_Edit.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", userPassword_Edit.getText().toString()));
//add data
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream in = response.getEntity().getContent();
StringBuilder stringbuilder = new StringBuilder();
BufferedReader bfrd = new BufferedReader(new InputStreamReader(in),1024);
String line;
while((line = bfrd.readLine()) != null)
stringbuilder.append(line);
downloadedString = stringbuilder.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("downloadedString:in login:::"+downloadedString);
Use AsyncTask
for your authentication and write the above method in the doInBackground()
.使用
AsyncTask
进行身份验证并在doInBackground()
编写上述方法。
EDIT编辑
You can follow below tutorials also,您也可以按照以下教程进行操作,
http://sarangasl.blogspot.in/2011/06/android-login-screen-using-httpclient.html http://sarangasl.blogspot.in/2011/06/android-login-screen-using-httpclient.html
http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
Hope it helps.希望能帮助到你。
I think, you should use POST method if you want to do something with user name and password.我认为,如果你想用用户名和密码做一些事情,你应该使用 POST 方法。 because when you use GET method, the password would be visible on the URI,
因为当您使用 GET 方法时,密码将在 URI 上可见,
https://samplesite.com/page/login?username=John&password=123
https://sampleste.com/page/login?name1=value1&name2=value2
Instead, you could use POST method to send user name and password values and in that case the URI would like below相反,您可以使用 POST 方法发送用户名和密码值,在这种情况下,URI 将如下所示
https://samplesite.com/page/login
And the values will be sent as,并且这些值将被发送为,
POST /page/login.asp HTTP/1.1
Host: samplesite.com
name1=value1&name2=value2
And you get below advantages on POST Method for secured transaction with server.您在与服务器进行安全交易的 POST 方法上获得以下优势。
Hi you can pass arguments to GET method in RESTfull like :嗨,您可以将参数传递给 RESTfull 中的 GET 方法,例如:
http://yoururl/<arg1>/<arg2>
eg.例如。
http://yoururl/abc/123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.