当前位置: 首页 > news >正文

南京制作网站公司哪家好福州本地推广

南京制作网站公司哪家好,福州本地推广,建筑设计网站issuu,公益事业做网站1.http://t.csdnimg.cn/96m2g 此链接提供测试代码; 2.http://t.csdnimg.cn/64iCC 此链接提供测试接口; 关于Webservice的基础部分不做赘述,下面贴上我的测试代码(属于动态调用Webservice): 1&#xff…

1.http://t.csdnimg.cn/96m2g

此链接提供测试代码;

2.http://t.csdnimg.cn/64iCC

此链接提供测试接口;

关于Webservice的基础部分不做赘述,下面贴上我的测试代码(属于动态调用Webservice):

1:http助手类

using System.Net;
using System.Text;
using System.Web;
using static System.Net.Mime.MediaTypeNames;namespace WebServiceGetWeather;public class HttpHelper
{private static HttpHelper m_Helper;/// <summary>/// 单例模式/// </summary>public static HttpHelper Helper{get { return m_Helper ?? (m_Helper = new HttpHelper()); }}/// <summary>/// 获取请求的数据/// </summary>/// <param name="strUrl">请求地址</param>/// <param name="requestMode">请求方式</param>/// <param name="parameters">参数</param>/// <param name="requestCoding">请求编码</param>/// <param name="responseCoding">响应编码</param>/// <param name="timeout">请求超时时间(毫秒)</param>/// <returns>请求成功响应信息,失败返回null</returns>public string GetResponseString(string strUrl, ERequestMode requestMode,  Dictionary<string,string> parameters, Encoding requestCoding, Encoding responseCoding, int timeout = 300){string url = VerifyUrl(strUrl);HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));HttpWebResponse webResponse = null;switch (requestMode){case ERequestMode.Get:webResponse = GetRequest(webRequest, timeout);break;case ERequestMode.Post:webResponse = PostRequest(webRequest, parameters, timeout, requestCoding);break;}if (webResponse != null && webResponse.StatusCode == HttpStatusCode.OK){using (Stream newStream = webResponse.GetResponseStream()){if (newStream != null)using (StreamReader reader = new StreamReader(newStream, responseCoding)){string result = reader.ReadToEnd();return result;}}}return null;}/// <summary>/// get 请求指定地址返回响应数据/// </summary>/// <param name="webRequest">请求</param>/// <param name="timeout">请求超时时间(毫秒)</param>/// <returns>返回:响应信息</returns>private HttpWebResponse GetRequest(HttpWebRequest webRequest, int timeout){try{webRequest.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01";webRequest.Headers.Add("Accept-Language", "zh-cn,en-US,en;q=0.5");webRequest.Headers.Add("Cache-Control", "no-cache");webRequest.UserAgent = "DefaultUserAgent";//webRequest.ContentType = "application / json";webRequest.Timeout = timeout;webRequest.Method = "GET";// 接收返回信息HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();return webResponse;}catch (Exception ex){return null;}}/// <summary>/// post 请求指定地址返回响应数据/// </summary>/// <param name="webRequest">请求</param>/// <param name="parameters">传入参数</param>/// <param name="timeout">请求超时时间(毫秒)</param>/// <param name="requestCoding">请求编码</param>/// <returns>返回:响应信息</returns>private HttpWebResponse PostRequest(HttpWebRequest webRequest, Dictionary<string, string> parameters, int timeout, Encoding requestCoding){try{// 拼接参数string postStr = string.Empty;if (parameters != null){parameters.All(o =>{if (string.IsNullOrEmpty(postStr))postStr = string.Format("{0}={1}", HttpUtility.UrlEncode(o.Key), HttpUtility.UrlEncode(o.Value));elsepostStr += string.Format("&{0}={1}", HttpUtility.UrlEncode(o.Key), HttpUtility.UrlEncode(o.Value));return true;});}byte[] byteArray = requestCoding.GetBytes(postStr);webRequest.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01";webRequest.Headers.Add("Accept-Language", "zh-cn,en-US,en;q=0.5");webRequest.Headers.Add("Cache-Control", "no-cache");webRequest.UserAgent = "DefaultUserAgent";//webRequest.Timeout = timeout;webRequest.ContentType = "application/x-www-form-urlencoded";webRequest.ContentLength = byteArray.Length;webRequest.Method = "POST";// 将参数写入流using (Stream newStream = webRequest.GetRequestStream()){newStream.Write(byteArray, 0, byteArray.Length);newStream.Close();}// 接收返回信息HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();return webResponse;}catch (Exception ex){return null;}}/// <summary>/// 验证URL/// </summary>/// <param name="url">待验证 URL</param>/// <returns></returns>private string VerifyUrl(string url){if (string.IsNullOrEmpty(url))throw new Exception("URL 地址不可以为空!");if (url.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase))return url;return string.Format("http://{0}", url);}
}
public enum ERequestMode
{Get,Post
}

2:调用方法

internal class Program{static void Main(string[] args){Console.WriteLine("Hello, World!");TestInvoke();}/// <summary>/// 测试调用/// </summary>static void TestInvoke(){//组织参数//<参数名,参数值>Dictionary<string, string> parameters = new Dictionary<string, string>();parameters.Add("byProvinceName", "北京");//getSupportCity是方法名称直接放在后面string url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity ";string _result = HttpHelper.Helper.GetResponseString(url, ERequestMode.Post, parameters, Encoding.Default, Encoding.UTF8);}

注意事项:

1:接口方法里面的案例参数要与代码中指定的一致:

 2:方法名直接放在url结尾:

string url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity ";

△   其中getSupportCity 就是方法名。

Dictionary<string, string> parameters = new Dictionary<string, string>();

△   其中parameters 的key是方法参数名,value是参数值。

先装一下原创,原作有想法评论或者私聊,即可更改。

http://www.yayakq.cn/news/757782/

相关文章:

  • 建设部网站官网建筑施工合同动漫网站首页设计
  • 外贸网站假设个人注册公司流程和费用标准
  • 南城网站建设公司案例joomla网站模板
  • 嘉峪关建设厅官方网站怎么建设小说网站
  • 京东联盟需要自己做网站吗如何优化推广网站
  • 网站安全证书过期怎么办室内设计师资格证
  • 营销网站是什么意思郑州高端建站公司
  • 寺院的网站怎么做四川网站建设那家好
  • 网站打不开怎么解决五种类型网站
  • 汉川网站制作化工厂网站建设
  • 自学建站网成都网站平面设计
  • 网站排名费用互联网站开发
  • 2016企业网站模板中文wordpress主题自定义打不开
  • 天天斗地主官方网站开发dede网站模板免费下载
  • 济南网站技术.net网站项目有哪些
  • 网站优化有什么用公司注册官方网站
  • wordpress strip tags优质的杭州网站优化
  • 淘宝联盟登记新网站伊宁网站建设
  • 在线设计网站排名网址你懂我意思正能量不用下载ios
  • 二维码制作网站廊坊做网站优化的公司
  • 网站做app企业网站建设浩森宇特
  • 燕郊做网站公司网易云音乐网站建设项目规划书
  • 网站申请微信支付无锡网站制作优化推广公司
  • 行政助手网站开发电销系统线路
  • 网站建设互联网推广wordpress 游客不可见
  • 3合1网站建设电话哔哩哔哩网页版下载视频
  • 涪城移动网站建设长春网站建设找新生科技
  • 企业网站建设的ppt电商平台app定制开发
  • 丽水市龙泉市网站建设公司网站做301排名会掉
  • 58同城建网站怎么做wordpress更好后台登录logo