网站百度普通收录 API提交代码(C#编写)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
// #region Post/Get请求数据 static
string domainUrl = “www.sunsharer.cn”;//站点
string secretKey = “bba43ace2106730f615b8l7aX8wH2”;//token,在百度站长工具里申请
string url;
string postUrl =“https://www.sunsharer.cn/”//提交的页面
string data;
protected void Page_Load(object sender, EventArgs e)
{
url = string.Format(“http://data.zz.baidu.com/urls?site={0}&token={1}”, domainUrl, secretKey);
data = SendRequest(url, postUrl);
Response.Write(data);
}

    public  string SendRequest(string requestUrl, string data)
    { 
        HttpWebRequest httpWebRequest = null;
        string returnData = "";
        try
        {
        
            httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
            httpWebRequest.Method =  WebRequestMethods.Http.Post;
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Proxy = null;
            httpWebRequest.KeepAlive = false;
            httpWebRequest.ServicePoint.UseNagleAlgorithm = false;
            httpWebRequest.AllowWriteStreamBuffering = false;
            httpWebRequest.Accept = "application/json";
            byte[] dataArray = Encoding.UTF8.GetBytes(data);
            httpWebRequest.ContentLength = dataArray.Length;

            using (Stream requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(dataArray, 0, dataArray.Length);
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                returnData = streamReader.ReadToEnd();

                streamReader.Close();
                httpWebResponse.Close();
            }

            httpWebRequest.Abort();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
           
         //   throw ex;
        }
        finally
        {
            if (httpWebRequest != null)
            {
                httpWebRequest.Abort();
                httpWebRequest = null;
            }
        }

        return returnData;
    }

}

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值