• HttpWebRequest post提交XMl参数请求,


    public string StartPing(string pingURL)//改为一个传参数URL
    {
    StreamReader sr = null;
    StringBuilder sb = new StringBuilder();
    string strOutput = string.Empty;
    string strXML = string.Empty;
    try
    {
    //HttpUtility.HtmlEncode
    strXML = BuildXML();
    int len = strXML.Length;
    ASCIIEncoding ascii = new ASCIIEncoding();
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(pingURL);
    webReq.Method = "POST";
    webReq.Timeout = 3000;//超时为3秒
    webReq.ContentType = "text/xml"; 
    //string contenttype = "application/x-www-form-urlencoded";//更网站该方法支持的类型要一致
    webReq.Credentials = CredentialCache.DefaultNetworkCredentials;
    webReq.ContentLength = len;
    
    Stream streamRequest = webReq.GetRequestStream();
    byte[] bt = ascii.GetBytes(strXML);
    streamRequest.Write(bt, 0, len);
    HttpWebResponse webRes = (HttpWebResponse)webReq.GetResponse();
    
    sr = new StreamReader(webRes.GetResponseStream(), Encoding.ASCII);
    string ret = sr.ReadToEnd();
    sb.AppendLine("\n");
    sb.AppendLine(ret);
    
    if (ret.IndexOf("<boolean>0</boolean>") >= 0)
    {
    sb.AppendLine("\n\t 服务提交成功!");
    }
    else if (ret.IndexOf("<boolean>1</boolean>") >= 0)
    {
    sb.AppendLine("\n\t 服务提交失败!");
    }
    if (sr != null)
    {
    sr.Close();
    }
    }
    catch (Exception ex)
    {
    sb.AppendLine(ex.Message);
    sb.AppendLine(ex.StackTrace);
    }
    finally
    {
    strOutput = sb.ToString();
    if (sr != null)
    {
    sr.Dispose();
    }
    }
    return strOutput;
    }
    
    private string BuildXML()
    {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<?xml version=\"1.0\"?>");
    sb.Append("<params>");
    sb.AppendFormat("<title>{0}", "公告标题");
    sb.Append("</title>");
    sb.AppendFormat("<content>{0}", "公告内容,base64加密的");
    sb.Append("</content>");
    
    sb.Append("<verify=be10c510b5f4f082dbd4268820cde895>");
    
    sb.Append("</params>");
    return sb.ToString();
    }
    

      

    下面是服务器端接收请求方法:

    加载事件里:
    //对应方法StartPing
                byte[] byts = new byte[Request.InputStream.Length];
                Request.InputStream.Read(byts, 0, byts.Length);
                string req = System.Text.Encoding.Default.GetString(byts);
                req = Server.UrlDecode(req);
                //下面是接收到的内容
                //<?xml version="1.0"?><params><title>????</title><content>?????base64???</content><verify=be10c510b5f4f082dbd4268820cde895></params>
                */
    

      

  • 相关阅读:
    putty加了密钥ssh不能登陆,PuTTY:server refused our key问题的解决(转)
    CentOS 7 yum 安装php5.6
    sqlite3.OperationalError: no such table: account_user
    python解决八皇后问题的方法
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 140: invalid continuation byte
    GET /static/plugins/bootstrap/css/bootstrap.css HTTP/1.1" 404 1718
    Java中wait()与notify()理解
    Javac可以编译,Java显示找不到或无法加载主类
    《剑指offer》第三十题:包含min函数的栈
    《剑指offer》第二十九题:顺时针打印矩阵
  • 原文地址:https://www.cnblogs.com/soundcode/p/2966268.html
Copyright © 2020-2023  润新知