Java 如何将参数传递给 RESTfull webservice 中的 GET 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23133665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:58:11  来源:igfitidea点击:

how to pass arguments to GET method in RESTfull webservice

javaandroidweb-servicesrest

提问by user3543997

I have to pass UserNameand passwordas arguments into GETmethod for validation.after processing I need to get response.so how can I pass value into RESTfulwebservice GETmethod?

我必须将UserNamepassword作为参数传递给GET方法进行验证。处理后我需要得到响应。那么如何将值传递给RESTfulwebserviceGET方法?

采纳答案by jgitter

Arguments are generally passed in a GET request as URL parameters. Without knowing what you've tried or where you're struggling, I can't really provide much more of an answer than that.

参数通常作为 URL 参数在 GET 请求中传递。在不知道您尝试过什么或在何处挣扎的情况下,我真的无法提供比这更多的答案。

--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/

回答by oleksii

To pass parameters in HTTP GET you should use a ?delimiter. Such as

要在 HTTP GET 中传递参数,您应该使用?分隔符。如

https://mywebsite.com/user/login?username=bob&password=123
https://mywebsite.com/user/login?paramname1=value1&paramname2=value2

Make sure to always use httpswith any sensitive data. You may also need to escape/encode both username and password to allow extended ASCII. If you need to support UNICODE you should consider using a POST request.

确保始终https与任何敏感数据一起使用。您可能还需要对用户名和密码进行转义/编码以允许扩展 ASCII。如果您需要支持 UNICODE,您应该考虑使用 POST 请求。

回答by Spring Breaker

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 AsyncTaskfor 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.

希望能帮助到你。

回答by ArunkumarB

I think, you should use POST method if you want to do something with user name and password. because when you use GET method, the password would be visible on the URI,

我认为,如果你想用用户名和密码做一些事情,你应该使用 POST 方法。因为当您使用 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 方法上获得以下优势。

  • It never cached
  • Requests will remain in the browser history
  • Requests cannot be bookmarked
  • Requests have no restrictions on data length
  • 它从不缓存
  • 请求将保留在浏览器历史记录中
  • 不能为请求添加书签
  • 请求对数据长度没有限制

回答by Mukesh Dabhi

Hi you can pass arguments to GET method in RESTfull like :

嗨,您可以将参数传递给 RESTfull 中的 GET 方法,例如:

 http://yoururl/<arg1>/<arg2>

eg.

例如。

 http://yoururl/abc/123