如何使用http标头发送http请求

Kut*_*tbi 8 android

提前致谢...

即时通讯使用此代码在http请求中设置http标头验证网址..

但我觉得有些什么是因为我无法得到回应......

响应仍然像"需要授权"......

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){
}
Run Code Online (Sandbox Code Playgroud)

plssss尽快帮助我

DAr*_*rkO 8

你的代码很好.你有http客户端设置权限和标题添加权限.但.您的基本身份验证错误.标题密钥(身份验证)是正确的,但值应为Basic + Base64.encode(用户名+":"+传递)

另外一个替代方案是下面的代码:

 httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));
Run Code Online (Sandbox Code Playgroud)

目标主机名可能为null,端口为-1.

但是如果你使用url连接,那么你将无法使用这个,所以标题也是可以接受的.

编辑:

这是你的问题:

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

什么是theese连接?

基本身份验证意味着添加base64编码的授权标头,该标头包含该单词 "Basic " + Base64.encodeToString("yourUsername"+":"+"yourPassword)

另一种方法是以相同的方式添加我在顶部粘贴凭证提供程序的方法