C# HttpWebRequest请求远程地址获取返回消息

时间:2022-03-02 04:11:12

HttpWebRequest请求远程地址获取返回消息

        /// <summary>
/// 请求远程Api获取响应返回字符串
/// </summary>
/// <param name="apiUrl">Api地址</param>
/// <param name="parameters">传递参数键值对</param>
/// <param name="contentType">内容类型默认application/x-www-form-urlencoded</param>
/// <param name="methord">请求方式默认POST</param>
/// <param name="timeout">超时时间默认300000</param>
/// <returns>响应字符串</returns>
static public object GetHttpWebResponseReturnString(string apiUrl, Dictionary<string, object> parameters, string contentType = "application/x-www-form-urlencoded", string methord = "POST", int timeout = )
{
string result = string.Empty;
string responseText = string.Empty;
try
{
if (string.IsNullOrEmpty(apiUrl))
{
return DNTRequest.GetResultJson(false, "请求apiURl为空", null);
} StringBuilder postData = new StringBuilder();
if (parameters != null && parameters.Count > )
{
foreach (var p in parameters)
{
if (postData.Length == )
{
postData.AppendFormat("{0}={1}", p.Key, p.Value);
}
else
{
postData.AppendFormat("&{0}={1}", p.Key, p.Value);
}
}
} ServicePointManager.DefaultConnectionLimit = int.MaxValue; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
myRequest.Proxy = null;
myRequest.Timeout = timeout;
myRequest.ServicePoint.MaxIdleTime = ;
if (!string.IsNullOrEmpty(contentType))
{
myRequest.ContentType = contentType;
}
myRequest.ServicePoint.Expect100Continue = false;
myRequest.Method = methord;
byte[] postByte = Encoding.UTF8.GetBytes(postData.ToString());
myRequest.ContentLength = postData.Length; using (Stream writer = myRequest.GetRequestStream())
{
writer.Write(postByte, , postData.Length);
} using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
{
responseText = reader.ReadToEnd();
}
}
if (!string.IsNullOrEmpty(responseText))
{
result = responseText;
}
else
{
result = "远程服务无响应,请稍后再试";
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
result = "请求异常,请稍后再试";
}
return result;
}

参考

C# HttpWebRequest请求远程地址获取返回消息的更多相关文章

  1. C&num; HttpWebRequest向远程地址Post文件

    HttpWebRequest向远程地址Post文件 /// <summary> /// 上传文件到远程服务器 /// </summary> /// <param name ...

  2. Resttemplate请求失败如何获取返回的json

    参考:https://blog.csdn.net/u011974797/article/details/82424004 https://www.cnblogs.com/liumz0323/p/106 ...

  3. PHP请求远程地址设置超时时间

    file_get_contents 请求超时设置 $timeout = array( 'http'=> array( 'timeout'=>5//设置一个超时时间,单位为秒 ) ); $c ...

  4. 根据第三方提供的webservice地址获取文件信息

    import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.en ...

  5. js&lowbar;html&lowbar;input中autocomplete&equals;&quot&semi;off&quot&semi;在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C&num;模拟httpwebrequest请求&lowbar;向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP&period;NET EF 使用LinqPad 快速学习Linq

    js_html_input中autocomplete="off"在chrom中失效的解决办法 分享网上的2种办法: 1-可以在不需要默认填写的input框中设置 autocompl ...

  6. C&num; HttpWebRequest 绝技 根据URL地址获取网页信息

    如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...

  7. 模拟HTTP请求获取返回值

    package org.jeecgframework.core.util; import java.io.BufferedReader; import java.io.IOException; imp ...

  8. HttpClient get和HttpClient Post请求的方式获取服务器的返回数据

    1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /*  * 演示通过HttpClie ...

  9. go 语言的库文件放在哪里?如何通过nginx代理后还能正确获取远程地址

    /usr/local/Cellar/go/1.5.1/libexec/src/ 他的RemoteAddr 是从哪里获取? func (c *conn) RemoteAddr() Addr { if ! ...

随机推荐

  1. 了解HTML CSS选择器操作和特性

    子选择器 在CSS样式表中, 有时候我们需要为一个选择器进行再次的选择, 比如要为某段落标签下的<span>标签进行样式设定(<span>标签必须为段落标签下的第一代子元素, ...

  2. Git Commit Template 提交模板

    多人协作开发一个项目时,版本控制工具是少不了的,git是linux 内核开发时引入的一个优秀代码管理工具,利用它能很好使团队协作完成一个项目.为了规范团队的代码提交,也方便出版本时的release n ...

  3. 在Linux手动把文件转码的方法,防止乱码出现

    iconv -f utf-8 -t gb2312 report.html  >  report_test.html

  4. 网络获取数据的Xml的Pull解析

    网络获取的XML的Pull解析 <?xml version="1.0" encoding="utf-8" ?> - <students> ...

  5. 流API--流的收集

    前面的一系列博客中,我们都是从一个集合中拿到一个流,但是有时候需要执行反操作,就是从流中获得集合.实际编码中,当我们处理完流后,我们通常想查看下结果,而不是将他们聚合成一个值.我们可以调用iterat ...

  6. Android Studio导出JavaDoc时中文乱码问题解决

    导出过程中,如果出现JavaDoc中文乱码的问题,可以在Other command line arguments栏目添加命令参数:-encoding UTF-8 -charset UTF-8(如果是G ...

  7. unigui&plus;fastreport报表打印

    unigui+fastreport报表打印   unigui+fastreport报表打印 FASTREPORT导出成PDF报表,UNIGUI的TUniURLFrame显示PDF报表并打印. func ...

  8. 使用github管理Eclipse分布式项目开发

    使用github管理Eclipse分布式项目开发 老关我在前面的博文(github管理iOS分布式项目开发)中介绍了github管理iOS分布式开发,今天老关将向大家介绍使用github管 理Ecli ...

  9. SQL Fundamentals&colon; 数据更新及事务处理(INSERT INTO&comma;UPDATE&comma;DELETE&comma;事务&comma;锁)

    SQL Fundamentals || Oracle SQL语言 在SQL语句中,数据操作语言DML由两部分组成,查询(DQL).更新操作(增加,修改,删除). 增加数据(INSERT INTO) 数 ...

  10. 【文档】三、Mysql Binlog事件类文件和类型

    在内部,服务器使用C++类文件来表示binlog事件.标准在log_event.h文件中,这些类的方法代码在log_event.cc中. log_event是基础类.其他的详细的事件子类都是来源于他. ...