C#后台给某网址传递参数,并接收返回值

            string url = string.Format(
                "http://bms.hichina.com/sms_gateway/sms_api?"
                + "user_id={0}&password={1}&mobile_phone={2}"
                + "&msg={3}&send_date={4}&subCode={5}"
            , string.Empty //用户id 
            , string.Empty //密码 
            , string.Empty //电话号码 
            , Server.UrlEncode(string.Empty) //短信消息 
            , Server.UrlEncode(string.Empty) //发送日期 
            , string.Empty //企业号 
            );
            System.Net.WebClient client = new System.Net.WebClient();
            client.Encoding = System.Text.Encoding.UTF8; //指定编码
            string reply = client.DownloadString(url);
            Response.Write(reply);
	//string url = "/SupplyAJAX.aspx";  
	//string strResult = GetRequestJsonString(url, "type=getcount");  

	#region 后台获取ashx返回的数据   
    ///    
    /// 后台获取ashx返回的数据   
    ///    
    /// 地址   
    /// 参数   
    ///    
    public static string GetRequestJsonString(string relativePath, string data)  
    {  
        string requestUrl = GetRequestUrl(relativePath, data);  
  
        try  
        {  
            WebRequest request = WebRequest.Create(requestUrl);  
            request.Method = "GET";  
  
            StreamReader jsonStream = new StreamReader(request.GetResponse().GetResponseStream());  
            string jsonObject = jsonStream.ReadToEnd();  
  
            return jsonObject;  
        }  
        catch  
        {  
            return string.Empty;  
        }  
    }  
  
    public static string GetRequestUrl(string relativePath, string data)  
    {  
        string absolutePath = HttpContext.Current.Request.Url.AbsoluteUri;  
        string hostNameAndPort = HttpContext.Current.Request.Url.Authority;  
        string applicationDir = HttpContext.Current.Request.ApplicationPath;  
        StringBuilder sbRequestUrl = new StringBuilder();  
        sbRequestUrl.Append(absolutePath.Substring(0, absolutePath.IndexOf(hostNameAndPort)));  
        sbRequestUrl.Append(hostNameAndPort);  
        sbRequestUrl.Append(applicationDir);  
        sbRequestUrl.Append(relativePath);  
        if (!string.IsNullOrEmpty(data))  
        {  
            sbRequestUrl.Append("?");  
            sbRequestUrl.Append(data);  
        }  
        return sbRequestUrl.ToString();  
    }  
    #endregion    
}  


转载于:https://www.cnblogs.com/smartsmile/archive/2012/11/26/6234367.html

你可能感兴趣的:(C#后台给某网址传递参数,并接收返回值)