使用了curl的库, 这样无论在windows或者在linux都可以使用.
win下的编程环境是TDM-GCC-64, 怎样安装,也是另一个话题。需要这个的请自行上网查询怎样安装.
linux 下是gcc环隐陵卜境,最好先安装curl开发包,目的就是需要curl.h等文件, 怎样安装,也是另一个话题。
废话不说,以下是正式程序.
#include
#include
#include
#include
struct string {
char *ptr
size_t len
}
void init_string(struct string *s) {
s->len = 0
s->ptr = malloc(s->len + 1)
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed ")
exit(EXIT_FAILURE)
}
s->ptr[0] = ''
}
size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
size_t new_len = s->len + size * nmemb
s->ptr = realloc()(s->ptr, new_len + 1)
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed ")
exit(EXIT_FAILURE)
}
memcpy(s->ptr + s->汪樱len, ptr, size*nmemb)
s->ptr[new_len] = ''
s->len = new_len
return size * nmemb
}
CURLcode curl_post_req(char *url, char *postParams,struct curl_slist *headers, char *response)
{
CURL *curl
curl = curl_easy_init()//初始化
// curl返回值
CURLcode res
if (curl)
{
struct string s
init_string(&s)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers)
//curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/post")
curl_easy_setopt(curl, CURLOPT_URL, url)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s)
curl_easy_setopt(curl, CURLOPT_POST, 1)//设置CURLOPT_POST之后必须带有POST数据
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams)
//不接收响应头数据0代表不接收 1代表接收
curl_easy_setopt(curl, CURLOPT_HEADER, 0)
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)
//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0)
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL)
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1)
//设置超时时间,以秒来计算 CURLOPT_CONNECTTIMEOUT是连接超时
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10)
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10)
// https ssl 时需要用到,如果是 http 可以注释掉
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L)
res = curl_easy_perform(curl)
//printf(" =>%s=>=>", s.ptr)
sprintf(response," =>%s ", s.ptr)
free(s.ptr)
curl_slist_free_all(headers)
}
curl_easy_cleanup(curl)
return res
}
int main(int argc, const char *argv[])
{
// http 请求头, 构造
printf(" start... ")
struct curl_slist *headers1 = NULL
headers1 = curl_slist_append(headers1, "User-Agent: Mozilla/5.0 (Windows NT 10.0WOW64Trident/7.0rv:11.0) like Gecko")
headers1 = curl_slist_append(headers1, "Content-Type:application/x-www-form-urlencodedcharset=UTF-8")
char url_post0[100] = "https://www.lpfrx.com/"
// 查找的字符串 : delphi
char paramsLogin0[100] = "s=delphi"
char resPost0[40960] = ""
CURLcode res3 = curl_post_req(url_post0, paramsLogin0, headers1,resPost0)
if (res3 == CURLE_OK)
{
printf("data: %s" ,resPost0)
}
printf(" end... ")
return 0
}
// win 下cmd下运行乱码,请先执行 chcp 65001 转成 utf8. 默认是chcp 936
// win: 运行目录下需要zlib.dll libcurl.dll
// curl-Library 是我自己的目录,放在当前的程序目录下,win下的curl.h 也是需要自己去找,如果有python编程环境的话,也安装了curl库的话,应该可能会有curl.h的库路径
// win: gcc -o curlpostlpfrx curlpost_lpfrx.c -I ./curl-Library/include -L ./curl-Library/lib -lcurl
//linux: gcc -o curlpostlpfrx curlpost_lpfrx.c -lcurl
使用POST请求获取结果
2.1 创建LoginHandler.aspx处理页面
[csharp] view plain copy
protected void Page_Load(object sender, EventArgs e)
{
string result = ""
string userName = Request.Form["UserName"]
string 宏缓password = Request.Form["Password"]
if (userName == "admin" && password == "123")
{
result = "登陆成功"
}
else
{
result = "登陆失棚绝蚂败"
}
Response.Write(result)
}
2.2 编写POST请求与获取结果方法
[csharp] view plain copy
/// <summary>
/// POST请求与获取结果
/// </summary>链埋
public static string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = postDataStr.Length
StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII)
writer.Write(postDataStr)
writer.Flush()
HttpWebResponse response = (HttpWebResponse)request.GetResponse()
string encoding = response.ContentEncoding
if (encoding == null || encoding.Length < 1) {
encoding = "UTF-8" //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding))
string retString = reader.ReadToEnd()
return retString
}
2.3 调用测试
[csharp] view plain copy
static void Main(string[] args)
{
string url = "http://www.mystudy.cn/LoginHandler.aspx"
string data = "UserName=admin&Password=123"
string result = HttpPost(url, data)
Console.WriteLine(result)
Console.ReadLine()
}
一个http请求包括三个部分铅返,分别为请求行,请求报头(请求头),消息主体(请求体),类似以下这样:
HTTP协议规定post提交的数据必须放在消息主体中,但是协议并没有规定必须皮悉使用什么编码方式。服务端通过是根据请求头中的Content-Type字段来获知请求中的消息主体是用何种方式进行编码,再对消息主体进行解析。具体的编码方式包括
1. 以form形式发送post请求
Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然燃激乎后传给requests.post()的data参数即可。
2. 以json形式发送post请求
可以将一json串传给requests.post()的data参数,
3. 以multipart形式发送post请求
Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files参数即可。
输出:
{“args”: {},
“data”: “”,
“files”: {
“file”: “Hello world!”
},
“form”: {},
“headers”: {……
“Content-Type”: “multipart/form-data boundary=467e443f4c3d403c8559e2ebd009bf4a”,
……
},
“json”: null,
……
}
---------------------
作者:weixin_40283480
来源:CSDN
原文:https://blog.csdn.net/weixin_40283480/article/details/79208413
版权声明:本文为博主原创文章,转载请附上博文链接!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)