2013-07-30 206 views
-1

我正在制作一个android应用程序,它使用编码的JSON从网页中检索数据。在我进入网页浏览器的页面时,它要求我输入我的用户名和密码进行服务器身份验证。我如何做到这一点,当我做一个httpPost;它会发送一个用户名和密码,所以我可以访问该页面。Android httpClient服务器身份验证

它的这种身份认证http://www.wpwhitesecurity.com/wp-content/uploads/2012/08/web-server-authentication-dialogue-box.png

的我该怎么办呢?这里是我的代码:

  DefaultHttpClient httpClient = new DefaultHttpClient(); 


     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = null; 
     try { 
      httpResponse = httpClient.execute(httpPost); 
     } catch (ClientProtocolException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } catch (IOException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     try { 
      is = httpEntity.getContent(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
+0

你想用户名和密码到服务器与url..isn't它? –

+0

它的这种服务器用户名和通过http://www.wpwhitesecurity.com/wp-content/uploads/2012/08/web-server-authentication-dialogue-box.png –

回答

0

您可以像这样把你的用户名和密码:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair(your_username_key_string, your_username)); 
nameValuePairs.add(new BasicNameValuePair(your_password_key_string, your_password));    

HttpPost oHttpPost = new HttpPost(your_server_url); 
oHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
+0

your_username_key_string和your_username之间的区别是什么?我不知道第一个是什么? –

+0

尽管NameValuePair是一个键 - 值对,那么your_username_key_string是字符串格式的关键字,而your_username是该键的字符串格式的值,您要发布 –

0

好......这样你就可以通过它:

DefaultHttpClient httpClient = new DefaultHttpClient(); 

String url = "yoururl"+"username="+YourUsername+"password="+Your Paasword; 

    HttpPost httpPost = new HttpPost(url); 

    HttpResponse httpResponse = null; 
    try { 
     httpResponse = httpClient.execute(httpPost); 
    } catch (ClientProtocolException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } catch (IOException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } 
    HttpEntity httpEntity = httpResponse.getEntity(); 
    try { 
     is = httpEntity.getContent(); 
    } catch (IllegalStateException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
0

添加参数到帖子:

final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("username", username)); 
nameValuePairs.add(new BasicNameValuePair("password", password)); 
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 

这是assu你的登录表单看起来像:

input type="text" name="username" 
input type="password" name="password" 
+0

它的服务器用户名和密码就像这个http://www.wpwhitesecurity。 COM /可湿性粉剂内容/上传/ 2012/08/Web服务器的身份验证 - 对话 - box.png –