简体   繁体   English

如何从 HTTP 响应标头字符串中提取参数?

[英]How do I extract parameters from an HTTP response header string?

I need to authenticate against a proxy server, and after the first HTTP request the I need to parse the proxy-authenticate header string to get the relevant values.我需要对代理服务器进行身份验证,并且在第一个 HTTP 请求之后,我需要解析 proxy-authenticate 标头字符串以获取相关值。

The response headers look something like this,响应头看起来像这样,

{ 'content-type': 'text/plain',
  'proxy-authenticate': 'Digest realm="zippy", qop="auth",nonce="c1e1c76b5df5a8cdc921b48d6a7b5493", algorithm="MD5", stale="false"',
   date: 'Thu, 21 Apr 2016 00:19:28 GMT',
   connection: 'close',
  'transfer-encoding': 'chunked' }

I want to extract the proxy-authenticate parameters (ie realm, qop, etc.), from the string.我想从字符串中提取代理身份验证参数(即领域、qop 等)。

It seems like there must be some super simple way to do this, but I'm just not finding it.似乎必须有一些超级简单的方法来做到这一点,但我只是没有找到它。

Simply extract your key from the JSON, and split the value by , .只需从 JSON 中提取您的密钥,然后将值拆分为, Then again split each value of resulting array using = .然后再次使用=拆分结果数组的每个值。

$(document).ready(function() {
  var data = {
    'content-type': 'text/plain',
    'proxy-authenticate': 'Digest realm="zippy", qop="auth",nonce="c1e1c76b5df5a8cdc921b48d6a7b5493", algorithm="MD5", stale="false"',
    date: 'Thu, 21 Apr 2016 00:19:28 GMT',
    connection: 'close',
    'transfer-encoding': 'chunked'
  };

  var header = data['proxy-authenticate'];
  var contents = header.split(',');

  var pairs = {};
  $.each(contents, function(index, value) {

    var pair = value.split('=');
    pairs[pair[0]] = pair[1];
  });

  $('div').html(JSON.stringify(pairs));
});

Here is a demo .这是一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM