public static str SendPostWithHeader(str url, str param, System.Net.WebHeaderCollection _headers, str contentType = "application/json", str encoding = "utf-8")
{
    System.Net.HttpWebRequest request = null;
    System.IO.Stream requestStream;
    str  dataString;
    if (strStartsWith(url, "https"))
    {
        request = System.Net.WebRequest::Create(url)as System.Net.HttpWebRequest;
    }
    else
    {
        request = System.Net.WebRequest::CreateHttp(url)as System.Net.HttpWebRequest;
    }
    request.Method = "POST";
    request.Headers = _headers;
    request.ContentType = contentType;
    System.Byte[] postData = System.Text.Encoding::GetEncoding( encoding ).GetBytes(param);
    request.ContentLength = postData.Length;
    try
    {
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData,0,postData.Length);
        outputStream.Close();

        System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
        System.IO.Stream responseStream = response.GetResponseStream();
        System.IO.StreamReader reader=new System.IO.StreamReader(responseStream,System.Text.Encoding::GetEncoding("GBK"));
        dataString = reader.ReadToEnd();
        reader.Close();

        if(!dataString)
            throw error("接口数据返回异常");
    }
    catch (Exception::CLRError)
    {
        System.Exception ex;
        ex = ClrInterop::getLastException();
        if (ex != null)
        {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                dataString = ex.ToString();
            }
        }
    }
    catch
    {
        SysInfoLogEnumerator infoLogEnum;
        infoLogEnum = SysInfoLogEnumerator::newData(infolog.copy(1, infologLine()));
        while(infoLogEnum.moveNext())
        {
            dataString += infoLogEnum.currentMessage();
        }
    }

    return dataString;
}

标签: none

添加新评论