我在将数据发送到网站和获取Python响应时遇到问题。我也看到过类似的问题,但似乎没有一个能实现我的目标。
这是我要移植到Python的C代码:
static void Request(Uri selectedUri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(selectedUri);
request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
request.Method = "GET";
request.Timeout = (int)Timeout.TotalMilliseconds;
request.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
request.CachePolicy = CachePolicy;
request.UserAgent = UserAgent;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
{
string responseText = responseReader.ReadToEnd();
File.WriteAllText(UrlFileName, responseText.Trim(), Encoding.ASCII);
}
}
}
下面是我在Python中的尝试:
def request():
web = httplib.HTTPConnection('https://someurl.com');
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
web.request("GET", "/heartbeat.jsp", headers);
response = web.getresponse();
stream = ""; #something is wrong here
任何帮助都将不胜感激!
你可以使用urllib2
也可以使用httplib
或者requests library
在Python中,可以使用
urllib2
(http://docs.python.org/2/library/urllib2.html)为您完成所有这些工作。很简单:
将打印接收到的HTTP响应。
要传递GET/POST参数,可以使用
urllib.urlencode()
函数。有关详细信息,请参阅Official Urllib2 Tutorial相关问题 更多 >
编程相关推荐