开发者

URL and http get parameters

开发者 https://www.devze.com 2023-03-01 02:28 出处:网络
what is the correct way to parse a URL for the HTTP get arguments. Fo开发者_开发问答r instance, google search

what is the correct way to parse a URL for the HTTP get arguments. Fo开发者_开发问答r instance, google search

http://www.google.com/search?hl=en&source=hp&biw=1422&bih=700&q=blabla&btnG=Google+Search&aq=f&aqi=&aql=&oq=

Wireshark shows

GET /search?hl=en&source=hp&biw=1422&bih=700&q=blabla&btnG=Google+Search&aq=f&aqi=&aql=&oq

Parsing logic?


I think you want to parse the Request-URI.

Here's one way to do it on C++:

#include <iostream>
#include <string>

// Returns the relative Request-URI from url. For example, given the url
// "http://www.google.com/search?q=xyz", it will return "/search?q=xyz".
//
// url need not be prefixed with a protocol; i.e. "google.com" is valid.
//
// If url contains a protocol (i.e. "http://"), the Request-URI begins with the
// first "/" after the protocol. Otherwise, the Request-URI begins with the
// first "/".
//
// If url does not contain a Request-URI, its Request-URI is "/", the server
// root.
std::string ParseRequestUri(const std::string& url) {
  const std::string protocol_identifier("://");

  std::size_t pos = url.find(protocol_identifier);

  if (pos != std::string::npos)
    pos = url.find_first_of("/", pos + protocol_identifier.length());
  else
    pos = url.find_first_of("/");

  if (pos != std::string::npos)
    return url.substr(pos);

  return "/";
}

int main() {
  const std::string test_url = "http://google.com/search?hl=en&source=hp&biw=1422&bih=700&q=blabla&btnG=Google+Search&aq=f&aqi=&aql=&oq=";
  const std::string test_url2 = "example.com/path/to/foo/bar/baz.tar.gz";

  std::cout << ParseRequestUri(test_url) << std::endl;
  std::cout << ParseRequestUri(test_url2) << std::endl;
}

The above code will output:

/search?hl=en&source=hp&biw=1422&bih=700&q=blabla&btnG=Google+Search&aq=f&aqi=&aql=&oq=
/path/to/foo/bar/baz.tar.gz


the querystring is an array of key-value pairs. You could loop through them getting key and respective value. If you specify what programming language you use, i'll be able to provide code example

0

精彩评论

暂无评论...
验证码 换一张
取 消