如何使用HTTP标头发送HTTP请求 [英] how to send http request using http header

查看:128
本文介绍了如何使用HTTP标头发送HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢...

使用此$ C $下的HTTP标头中的HTTP请求的URL进行身份验证IM。

i m using this code for set http header in http request for authenticate url..

但我觉得有些东西是mising这就是为什么我不能得到回应......

but i think some what is mising thats why i could not get response...

响应仍然来自像需要authoriazation......

response still comes like "authoriazation required"...

httpParameters = new BasicHttpParams();
String auth = android.util.Base64.encodeToString(
    ("florin999@zitec.ro" + ":" + "test2323" + ":" + "zitec"
    + ":" + "7716099f468cc71670b68cf4b3ba545c5760baa7")
    .getBytes("UTF-8"), android.util.Base64.NO_WRAP);

HttpConnectionParams.setSoTimeout(httpParameters, 0);
client = new DefaultHttpClient(httpParameters);
String getURL = "URL here";
get = new HttpGet(getURL);
get.addHeader("Authorization", "Basic "+ auth);
// get.addHeader("X-ZFWS-Accept", "text/json");

HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
    //do something with the response
    //Toast.makeText(Login.this.getApplicationContext(),EntityUtils.toString(resEntityGet), Toast.LENGTH_SHORT).show();
    String s = EntityUtils.toString(resEntityGet);
    tv1.setText(s);
}
}catch(Exception e){
}

plssss帮助我尽快

plssss help me as soon as possible

推荐答案

尤尔code是罚款。你有HTTP客户端安装正确,并添加正确的标题。但。你的基本身份验证是错误的。标题密钥(认证)是正确的,但该值应该是基本+ Base64.en code(用户名+:+通)

Yor code is fine. you have the http client setup right and the header added right. but. your basic authentication is wrong. the header key (Authentication) is right but the value should be Basic + Base64.encode(username+":"+pass)

也替代是如下因素code:

also the alternative to that is the folowing code:

 httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));

目标主机名可能是零和端口-1。

The target Hostname is probably null and the port -1.

不过这一次,你将无法如果您使用的URL连接,所以一个头也是可以接受的使用。

but this one you wont be able to use if you are using url connection so a header is also acceptable.

编辑:

这是您的问题:

String auth = android.util.Base64.encodeToString(
    ("florin999@zitec.ro" + ":" + "test2323" + ":" + "zitec"
    + ":" + "7716099f468cc71670b68cf4b3ba545c5760baa7")
    .getBytes("UTF-8"), android.util.Base64.NO_WRAP);

什么是所有theese串联??

What are all theese concatenations??

基本身份验证是指将一个base64连接codeD Authorization头该字的consisnt 基本+ Base64.en codeToString(yourUsername+:+你的密码)

Basic authentication means adding a base64 encoded Authorization header which consisnt of the word "Basic " + Base64.encodeToString("yourUsername"+":"+"yourPassword)

另一种方法是添加方法i贴在有关凭据提供顶部同样的方式

The alternative is adding the method i pasted at the top about the credentials provider the same way

这篇关于如何使用HTTP标头发送HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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