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# 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_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq

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

  6. C# 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. dos切换其他目录加参数/D

    D:\>cd /D c:\Windows c:\Windows> 不加参数/D 无法切换到另一个盘符

  2. input控件的checkbox属性自定义勾选框

    思路 首先隐藏input默认勾选框    通过绑定label标签,设置label的样式来设置勾选框 效果 在这里插入图片描述代码 # html中input的checkbox定义,使用for循环创建多个 ...

  3. mysql主从复制原理及步骤

    原理: 1master开启bin-log功能,日志文件用于记录数据库的读写增删2需要开启3个线程,master IO线程,slave开启 IO线程 SQL线程,3Slave 通过IO线程连接maste ...

  4. 最大生成树+map实现技巧

    POJ2263 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include< ...

  5. 解决spring-boot 各版本包冲突兼容的方法

    思路        在微服务盛行的当下,spring boot 流行程度已经家喻户晓.但同时,随着spring boot 快速迭代,出现了很多版本,比如当前已经推出了2.2.x-SNAPSHOT/ , ...

  6. 有关react-native的最常用的库(文件、样式、UI组件)

    一.对文件的处理 1.react-native-fs 2.react-native-file-selector 3.MaterialFilePicker 二.React-Native 样式指南 1.r ...

  7. openstack基于卷快照恢复卷

    openstack基于卷快照恢复卷 基于P版本,对卷基于快照进行恢复的源码分析 1.特性描述 在pike版本中,openstack官网增加了一个新特性,Cinder volume revert to ...

  8. [Python]统计1个元素在列表中的出现次数

    使用列表自带的count方法: list.count(element) 示例:  列表a,有4个元素,其中值1出现3次 In []: a=[,,,] In []: a Out[]: [, , , ] ...

  9. 007-IP报文协议

    一.概述 IP协议是将多个包交换网络连接起来,它在源地址和目的地址之间传送一种称之为数据包的东西,它还提供对数据大小的重新组装功能,以适应不同网络对包大小的要求. IP不提供可靠的传输服务,它不提供端 ...

  10. SVN错误信息汇总

    svn错误信息  # # Simplified Chinese translation for subversion package # This file is distributed under ...