我在哪里把REST客户端身份验证数据的查询? [英] Where do I put the REST Client Authentication Data in the Query?

查看:91
本文介绍了我在哪里把REST客户端身份验证数据的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Android应用程序REST API这是由我的客户创建工作。下面的文字是刚刚从客户端为我们提供PDF格式复制。

-

在此示例中,创建一个新的用户。
一个可能的请求给服务器的各部分如下所示:

信息内容的一部分。

 标题POST {URL- preFIX} / REST /用户
内容类型:应用程序/ XML
内容长度:205
身体lt;请求>
  <客户端>
    <&ID GT; XY< / ID>
    <名称>&MYNAME LT; /名称>
    <密码和GT;&MYPASSWORD LT; /密码>
  < /客户>
  <使用者>
    <名称>&名为myUsername LT; /名称>
    <密码和GT; myUserPassword< /密码>
    <&的groupId GT; 12345< /的groupId>
  < /使用者名称>
< /请求>

-

寻找和研究之后,我又知道,可能要求code(在Java中)可能是:

 网​​址URL =新URL(\"http://api.example.com/rest/user/?name=myUserName&password=myUserPassword&groupId=12345\");
            HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();
            conn.setDoOutput(真);
            conn.setRequestMethod(邮报);
            OutputStreamWriter出=新OutputStreamWriter(conn.getOutputStream());
            out.write(respose内容:);
            out.close();

从他们提供的PDF手册,我知道了,的每个请求到服务器,客户端(这就是我)的传输认证数据。

我的问题是,你在哪里我把身份验证数据在查询字符串?请帮我在这。

编辑:张贴低于code作为请求后:

  DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HttpPost httpPost =新HttpPost(http://api.example.com/rest/user/?name=Foysal&password=123456&groupid=12345);
            httpPost.addHeader(接受,文本/ XML);
            httpPost.setHeader(内容类型,应用程序/ XML的,字符集= UTF-8);
            清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
            nameValuePairs.add(新BasicNameValuePair(姓名,APIappDevAccount));
            nameValuePairs.add(新BasicNameValuePair(密码,123456));
            httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            的HttpParams PARAMS =新BasicHttpParams();
            HttpConnectionParams.setStaleCheckingEnabled(参数,可以假);
            HttpConnectionParams.setConnectionTimeout(PARAMS,5000);
            HttpConnectionParams.setSoTimeout(PARAMS,5000);
            httpClient.setParams(PARAMS);
            httpClient.getParams()的setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109)。
            HTT presponse响应= httpClient.execute(httpPost);
            InputStream为= response.getEntity()的getContent()。
            ByteArrayOutputStream OS =新ByteArrayOutputStream();
            的byte [] buf中;
            INT ByteRead;
            BUF =新的字节[1024];
            字符串XMLDATA = NULL;
            双总计TOTALSIZE = 0;
            而((ByteRead = is.​​read(BUF,0,buf.length))!= - 1){
                os.write(BUF,0,ByteRead);
                总计TOTALSIZE + = ByteRead;
            }
            XMLDATA = os.toString();
            os.close();
            is.close();



但我得到的回应是:


  

404
  未找到
  

未找到

请求
  URL / REST /用户/没有被这一发现
  服务器。


的Apache / 2.2.6
  (Fedora的)DAV / 2的mod_ssl / 2.2.6
  OpenSSL的/ 0.9.8b在服务器
  api.example.com端口80
  



像他们希望你发布一个XML文档,并把认证在
解决方案

在我看来。没有太大的REST API的(大部分REST API的不需要的XML文档)。

您需要使用conn.getOutputStream()访问该文档发送到服务器,并使用conn.getInputStream()来读取响应。

所以,你需要创建像他们展示一个XML文档:

 <请求>
  <客户端>
    <&ID GT; XY< / ID>
    <名称>&MYNAME LT; /名称>
    <密码和GT;&MYPASSWORD LT; /密码>
  < /客户>
  <使用者>
    <名称>&名为myUsername LT; /名称>
    <密码和GT; myUserPassword< /密码>
    <&的groupId GT; 12345< /的groupId>
  < /使用者名称>
< /请求>

,然后把它在你的POST:

  conn.setRequestProperty(内容类型,文本/ XML);
out.write(requestDoc); //其中requestDoc是包含XML字符串。
了out.flush();
out.close();

I need to work with REST api in android application which is created by my client. Below text is just copied from the pdf the client provides us.

--
In this example, a new user is created. The parts of a possible request to the server is shown below:

Message part Contents

Header POST {url-prefix}/rest/user
Content-Type: application/xml
Content-Length: 205
Body <request>
  <client>
    <id>XY</id>
    <name>myName</name>
    <password>myPassword</password>
  </client>
  <user>
    <name>myUserName</name>
    <password>myUserPassword</password>
    <groupId>12345</groupId>
  </user>
</request>

--
After searching and studying, I come to know that, the possible request code (in Java) might be:

URL url=new URL("http://api.example.com/rest/user/?name=myUserName&password=myUserPassword&groupId=12345");
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("Post");
            OutputStreamWriter out=new OutputStreamWriter(conn.getOutputStream());
            out.write("respose content:");
            out.close();

From the pdf manual they provide, I got to know, for every request to the server, the client (thats me) has to transmit the authentication data.
My question is, where do I put the authentication data in the query string? Please help me on this.

Edit:After posting the below code as request:

            DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://api.example.com/rest/user/?name=Foysal&password=123456&groupid=12345");
            httpPost.addHeader("Accept", "text/xml");
            httpPost.setHeader("Content-Type","application/xml;charset=UTF-8");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("name", "APIappDevAccount"));
            nameValuePairs.add(new BasicNameValuePair("password", "123456"));           
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setStaleCheckingEnabled(params, false);
            HttpConnectionParams.setConnectionTimeout(params, 5000);
            HttpConnectionParams.setSoTimeout(params, 5000);
            httpClient.setParams(params);
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            HttpResponse response = httpClient.execute(httpPost);
            InputStream is = response.getEntity().getContent();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] buf;
            int ByteRead;
            buf = new byte[1024];
            String xmldata = null;
            double totalSize = 0;
            while ((ByteRead = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, ByteRead);
                totalSize += ByteRead;                      
            }
            xmldata =  os.toString();
            os.close();
            is.close();


But I got the response as:

404 Not Found

Not Found

The requested URL /rest/user/ was not found on this server.


Apache/2.2.6 (Fedora) DAV/2 mod_ssl/2.2.6 OpenSSL/0.9.8b Server at api.example.com Port 80

解决方案

Looks to me like they want you to POST an XML document and put the authentication in that. Not much of a REST API (most REST APIS don't require an XML document).

You need to use conn.getOutputStream() to send that doc to the server and use conn.getInputStream() to read the response.

So you would have to create the XML doc like the one they show:

<request>
  <client>
    <id>XY</id>
    <name>myName</name>
    <password>myPassword</password>
  </client>
  <user>
    <name>myUserName</name>
    <password>myUserPassword</password>
    <groupId>12345</groupId>
  </user>
</request>

And then send it in your POST:

conn.setRequestProperty ( "Content-Type", "text/xml" );
out.write(requestDoc); //where requestDoc is the String containing the XML.
out.flush();
out.close();

这篇关于我在哪里把REST客户端身份验证数据的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆