http调用速度慢

m0_37360669 2019-01-15 02:01:23
string htp = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
jsonfh = HttpPost(htp, jsonText);

如果本地调用速度很快,但是放到服务器上,调用需要15秒才能返回结果

public static string HttpPost(string url, string postData)
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
byte[] data = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
request.Method = "Post";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.KeepAlive = true;

Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);

HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
}

StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();

request.Abort();
response.Close();
reader.Dispose();
stream.Close();
stream.Dispose();

return content;
}
...全文
259 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
上帝的亲哥哥 2019-02-14
  • 打赏
  • 举报
回复
这个问题,我专门花时间研究过,我是在远程服务器做了一个Rest Web API接口,发现调用时间有时候达到15秒左右,微软给出的答案是在http请示时,有一次代理检查,你的应该第二次请示就会很快了,还用,这种通讯不都是用现场的组件吗,我调用RestSharp,禁止代理后,速度明显变快,代码分离给你:




stopwatch.Start();
string url = BaseUrl + @"/License/";

var request = new RestRequest(url, Method.GET) { RequestFormat = DataFormat.Json };

request.AddParameter("p_key", "OLMS_WXYT");
client = new RestClient();

IWebProxy webProXy = new WebProxy();
webProXy.Credentials = null;
client.Proxy = webProXy;
IRestResponse response = client.Execute(request);

if (!string.IsNullOrEmpty(response.Content))
{
string strJson = response.Content;
GetNormalJson(ref strJson);

var model = JsonConvert.DeserializeObject<LicenseModel>(strJson);

AppendText(strJson);
stopwatch.Stop();
SoulRed 2019-01-15
  • 打赏
  • 举报
回复
天知道你是不是用的土豆服务器
大鱼> 2019-01-15
  • 打赏
  • 举报
回复
服务器进出带宽怎么样?应该考虑你的服务器与你本地环境的差异,主要是网络差异
  • 打赏
  • 举报
回复
自己在服务器上ping这个地址的耗时吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧