POST推送

B/S系统间消息交互可能会有webservice、wcf、POST推送、MQ消息、socket,当然MQ消息和socket消息队列另说; 今天主要是写POST推送,主要代码: 

 /// <summary>
        /// POST推送  
        /// </summary>
        /// <param name="postUrl">推送路劲</param>
        /// <param name="paramData">参数</param>
        /// <param name="dataEncode">Encoding.UTF8</param>
        /// <returns></returns>
        public static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);

                ret = sr.ReadToEnd();
                //ret = "Success";

                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                ret = "Error:" + ex.Message;
            }
            return ret;
        }
很简单,对吧!!
小伙伴们,注意一下 调用的时候:参数前一定要加上 "reqXml="    reqXml是参数名。

### 百度收录 POST 推送 示例代码 对于百度收录的POST推送,可以采用多种编程语言实现。下面提供了一个基于PHP的具体实例,该实例展示了如何利用PHP来调用百度的实时推送API。 #### 使用 PHP 实现百度收录 POST 推送 为了简化集成过程,可以直接将如下所示的函数嵌入到WordPress主题中的`functions.php`文件内: ```php function baidu_submit($url) { $ch = curl_init(); $options = array( CURLOPT_URL => "http://data.zz.baidu.com/urls?site=https://yourdomain.com&token=YourToken", CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", (array)$url), CURLOPT_HTTPHEADER => array('Content-Type: text/plain') ); curl_setopt_array($ch, $options); $result = curl_exec($ch); return $result; } add_action('save_post', 'auto_baidu_submit'); function auto_baidu_submit($post_id){ if (!wp_is_post_revision($post_id)) { $post_url = get_permalink($post_id); echo baidu_submit($post_url); } } ``` 此段脚本会在每次发布新文章时自动触发并向百度提交链接[^1]。 另外,在Python环境下也可以轻松完成同样的任务。这里给出一段用于推送站点地图(Sitemap)至百度搜索引擎的例子: ```python import requests from bs4 import BeautifulSoup def submit_sitemap_to_baidu(sitemap_url, api_key): sitemap_content = requests.get(sitemap_url).text soup = BeautifulSoup(sitemap_content,'xml') urls = [loc.text for loc in soup.find_all('loc')] url_str = '\n'.join(urls) post_data = {'sitename': 'https://example.com','token':'{0}'.format(api_key)} headers={'Content-Type': 'application/x-www-form-urlencoded'} response = requests.post('http://data.zz.baidu.com/urls', data=url_str.encode(), params=post_data, headers=headers) print(response.status_code,response.content.decode()) ``` 上述Python程序首先获取XML格式的网站地图内容,并从中解析出所有的URL列表;接着把这些URL作为纯文本形式的数据体,连同必要的参数一起发送给百度服务器以请求加速这些网页被索引的速度[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值