- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我让自己的单元使用 winsocket 从网络服务器获取 POST 请求。
这是我的单位:
unit uGetPost;
interface
uses
Winsock,
SysUtils,
Windows;
function GetPost(CompleteURL, PostData : String; var Results : String ; Port: Integer = 80) : Integer;
implementation
procedure RemoveCRLFFromEndAndBeginning (var s : String);
var
i : Integer;
begin
i := Length(s);
while (s[i] = #10) or (s[i] = #13) do begin
SetLength (s, i - 1);
dec (i);
end;
i := 1;
while (s[i] = #10) or (s[i] = #13) do begin
s := Copy (s, 2, Length(s));
inc (i);
end;
end;
function GetIpFromDns(HostName: string): string;
type
tAddr = array[0..100] of PInAddr;
pAddr = ^tAddr;
var
I: Integer;
WSA: TWSAData;
PHE: PHostEnt;
P: pAddr;
begin
Result := HostName;
WSAStartUp($101, WSA);
try
PHE := GetHostByName(pChar(HostName));
if (PHE <> nil) then
begin
P := pAddr(PHE^.h_addr_list);
I := 0;
while (P^[i] <> nil) do
begin
Result := (inet_nToa(P^[i]^));
Inc(I);
end;
end;
except
end;
WSACleanUp;
end;
function Parsing(Char, Str: string; Count: Integer): string;
var
i : Integer;
strResult : string;
begin
if Str[Length(Str)] <> Char then
Str := Str + Char;
for i := 1 to Count do
begin
strResult := Copy(Str, 0, Pos(Char, Str) - 1);
Str := Copy(Str, Pos(Char, Str) + 1, Length(Str));
end;
Result := strResult;
end;
function GetPost(CompleteURL, PostData : String; var Results : String ; Port: Integer = 80) : Integer;
// 1 = Complete Success
// 2 = No Content (Length found) or wrong GET/POST
// 3 = Host found but no php file
// 4 = Host not found (Total FAIL!);
var
WSA: TWSAData;
Sock: TSocket;
Addr: TSockAddrIn;
SendBuffer: String;
ReceiveBuffer: array[0..4096] of Char;
ReceivedBytes: Integer;
DNS, RemoteFilePath, FileName: string;
i: integer;
SentBytes: Integer;
ContentLength : Integer;
begin
result := 4;
DNS := Copy(CompleteURL, Pos('http://', CompleteURL) + 7, Length(CompleteURL));
RemoteFilePath := Copy(DNS, Pos('/', DNS), Length(DNS));
DNS := Copy(DNS, 1, Pos('/', DNS) - 1);
i := Length(RemoteFilePath);
while (RemoteFilePath[i] <> '/') do
begin
FileName := RemoteFilePath[i] + FileName;
Dec(i);
end;
WSAStartup($101, WSA);
Sock := Socket(AF_INET, SOCK_STREAM, 0);
Addr.sin_family := AF_INET;
if (Port < 1) or (Port > 65535) then Port := 80;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := inet_addr(PChar(GetIPfromDNS(PChar(DNS))));
if Connect(Sock, Addr, sizeof(Addr)) = 0 then begin
result := 3;
SendBuffer := 'POST ' + RemoteFilePath + ' HTTP/1.1' + #13#10 +
'Host: ' + DNS + #13#10 +
'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0' + #13#10 +
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' + #13#10 +
'Accept-Language: en-US,en;q=0.5' + #13#10 +
'Accept-Encoding: gzip, deflate' + #13#10 +
'Connection: close' + #13#10 +
'Cache-Control: max-age=0' + #13#10 +
'Content-Type: application/x-www-form-urlencoded' + #13#10 +
'Content-Length: ' + inttostr(Length(PostData)) + #13#10#13#10 +
PostData;
repeat
SentBytes := Send(Sock, SendBuffer[1 + SentBytes], Length(SendBuffer) - SentBytes, 0);
until SentBytes >= Length(SendBuffer);
repeat
ZeroMemory(@ReceiveBuffer, Sizeof(ReceiveBuffer));
ReceivedBytes := Recv(Sock, ReceiveBuffer, Sizeof(ReceiveBuffer), 0);
if ReceivedBytes > 0 then begin
Results := Results + ReceiveBuffer;
end;
until (ReceivedBytes <= 0);
CloseSocket(Sock);
end;
WSACleanup();
if Copy (Results, 10, 6) = '200 OK' then begin
result := 2;
if Pos ('Content-Length: ', Results) <> 0 then begin
i := 1;
while Parsing(#13, Results, i) <> '' do begin
if Pos ('Content-Length: ' , Parsing(#13, Results, i)) <> 0 then begin
ContentLength := strtoint (Copy(Parsing(#13, Results, i), 18, Length (Results)));
results := Copy (results,Length(results) - ContentLength + 1, ContentLength);
break;
end;
inc (i);
end;
if ContentLength <> 0 then begin
result := 1;
RemoveCRLFFromEndandBeginning (results);
end else begin
results := '';
end;
end;
end;
end;
end.
我在 VCL 应用程序中运行 GetPost 函数,如下所示:
var
Res : String;
begin
GetPost ('http://guest1320958.studio2.coderun.com/PHPTest/', 'GET=VERSION', Res);
ShowMessage (Res);
end;
结果如下:
HTTP/1.1 400 Bad Request Content-Type: text/html Date: Fri, 26 Oct 2012 18:56:03 GMT Connection: close Content-Length: 35
Bad Request (Invalid Verb)
如果我在这样的控制台应用程序中运行 SAME 函数:
program Project2;
{$APPTYPE CONSOLE}
uses
uGetPost;
var
Res : String;
begin
GetPost ('http://guest1320958.studio2.coderun.com/PHPTest/', 'GET=VERSION', Res);
writeln (Res);
readln;
end.
它工作得很好。
我的 PHP 代码是这样的:
<?php
if (isset($_POST["GET"])) {
$funcName = $_POST["GET"];
switch($funcName) {
case "VERSION":
echo "1.0";
break;
case "SOMETHINGELSE":
echo "...";
break;
case "ANDSOSON":
echo "...";
}
}
?>
我使用 www.coderun.com测试我的 php。
为什么它在 VLC 中不起作用?顺便说一句:如果您像这样在 VCL 的线程中运行函数 GetPost:
function MyThread ( p : pointer ) : Integer;stdcall;
var
Res : String;
begin
GetPost ('http://guest1320958.studio2.coderun.com/PHPTest/', 'GET=VERSION', Res);
MessageBoxA (0, pchar(Res), '', 0);
end;
procedure StartGetPost;
var
Dummy : DWORD;
begin
CreateThread(NIL,0, @MyThread, NIL,0, Dummy);
end;
...它突然工作...
这是为什么呢?可以请有人帮助我吗?谢谢。
编辑:以下是 wireshark 的结果: http://dl.dropbox.com/u/349314/transfer.pcapng
编辑:看起来实际传输 header 有问题:/
最佳答案
Wireshark 捕获显示,两次尝试之间的主要区别是 GUI 版本在 HTTP 数据中有一个额外的空字符。也就是说,在第一行 POST/PHPWebSite/HTTP/1.1
之前,有一个零字符。这解释了为什么服务器提示无效动词。
失败与在控制台或 GUI 模式下运行无关。相反,问题在于您在以下循环中使用了初始化变量:
repeat
SentBytes := Send(Sock, SendBuffer[1 + SentBytes], Length(SendBuffer) - SentBytes, 0);
until SentBytes >= Length(SendBuffer);
您还没有设置SentBytes
,但是您使用它来索引到SendBuffer
。在循环之前将其初始化为零。
编译器应该就未初始化的变量警告您。永远不要忽略来自编译器的消息,即使它“只是”提示或警告。
在 VCL 线程中,该局部变量显然占用了先前保存非零值(可能是 -1)的内存。在其他情况下,它显然得到值 0,并且您的代码似乎按预期工作。这就是所谓的未定义行为。
关于php - Delphi 代码在控制台中有效,但在 VLC 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13092908/
我遇到了一个奇怪的问题。我有这个: $(document).ready(function () {
我正在编写一个程序,它从列表中读取一些 ID,从中找出不同的 URL,然后将图像保存到我的 C: 驱动器中。 如果我在浏览器中导航到图像 URL,它们就会起作用。此外,如果我尝试从不同的服务器获取图像
我编写了一个 REST WCF RIA Silverlight 4.0 兼容服务,我可以从 javascript + jQuery.1.4.2.js + JSON2.js(当然,还可以从 .NET 4
我很确定这个网站实际上还没有得到回答。一劳永逸地,与 32 位有符号整数范围内的数字字符串匹配的最小正则表达式是什么,范围是 -2147483648至 2147483647 . 我必须使用正则表达式进
我有两个data.table;我想从那些与键匹配的元素中随机分配一个元素。我现在这样做的方式相当慢。 让我们具体点;这是一些示例数据: dt1<-data.table(id=sample(letter
我已经安装了 celery 、RabitMQ 和花。我可以浏览到花港。我有以下简单的工作人员,我可以将其附加到 celery 并从 python 程序调用: # -*- coding: utf-8 -
我正在使用 ScalaCheck 在 ScalaTest 中进行一些基于属性的测试。假设我想测试一个函数,f(x: Double): Double仅针对 x >= 0.0 定义的, 并返回 NaN对于
我想检查文件是否具有有效的 IMAGE_DOS_SIGNATURE (MZ) function isMZ(FileName : String) : boolean; var Signature: W
在 Herbert Schildt 的“Java:完整引用,第 9 版”中,有一个让我有点困惑的例子。它的关键点我无法理解可以概括为以下代码: class Test { public stat
我在工作中查看了一些代码,发现了一些我以前没有遇到过的东西: for (; ;) { // Some code here break; } 我们一直调用包含这个的函数,我最近才进去看看它是
在 Herbert Schildt 的“Java:完整引用,第 9 版”中,有一个让我有点困惑的例子。它的关键点我无法理解可以概括为以下代码: class Test { public stat
我试图编写一个函数,获取 2D 点矩阵和概率 p 并以概率 p 更改或交换每个点坐标 所以我问了一个question我试图使用二进制序列作为特定矩阵 swap_matrix=[[0,1],[1,0]]
这个问题在这里已经有了答案: Using / or \\ for folder paths in C# (5 个答案) 关闭 7 年前。 我在某个Class1中有这个功能: public v
PostgreSQL 10.4 我有一张 table : Column | Type ------------------------- id | integer| title
我正在 Postgresql 中编写一个函数,它将返回一些针对特定时区(输入)计算的指标。 示例结果: 主要问题是这只是一个指标。我需要从其他表中获取其他 9 个指标。 对于实现此目标的更简洁的方法有
我需要在 python 中模拟超几何分布(用于不替换采样元素的花哨词)。 设置:有一个装满人口许多弹珠的袋子。弹珠有两种类型,红色和绿色(在以下实现中,弹珠表示为 True 和 False)。从袋子中
我正在使用 MaterializeCSS 框架并动态填充文本输入。我遇到的一个问题是,在我关注该字段之前,valid 和 invalid css 类不会添加到我的字段中。 即使我调用 M.update
是否有重叠 2 个 div 的有效方法。 我有以下内容,但无法让它们重叠。 #top-border{width:100%; height:60px; background:url(image.jpg)
我希望你们中的一位能向我解释为什么编译器要求我在编译单元中重新定义一个静态固定长度数组,尽管我已经在头文件中这样做了。这是一个例子: 我的类.h: #ifndef MYCLASS_H #define
我正在使用旧线程发布试图解决相同问题的新代码。什么是安全 pickle ? this? socks .py from socket import socket from socket import A
我是一名优秀的程序员,十分优秀!