CAD233
CAD233
采纳率80%
2018-06-13 09:40 阅读 1.1k

C# 源代码是POST请求返回字符串流。由于现在返回的是一个网页平台,如何调整。请大神帮忙修改一下

20

public static String CreatePostHttpResponse(string url, IDictionary parameters, Encoding charset)
{
HttpWebRequest request = null;
//HTTPSQ请求

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = DefaultUserAgent;
//如果需要POST数据

if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = charset.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}

     Stream outstream = request.GetResponse().GetResponseStream();   //获取响应的字符串流  
      StreamReader sr = new StreamReader(outstream); //创建一个stream读取流  
        return sr.ReadToEnd(); 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    yuhugejile NominTal 2018-06-14 06:50

    我有个封装好的。你拿过去直接用就可以了。
    代码在github:
    github.com/Dhugjil/DFrame/blob/master/DFrame.Common/HttpService.cs
    封装在common层里。
    干活,全是好东西哦,可以拿过去学习。
    不用谢,采纳就好。

    点赞 评论 复制链接分享
  • showbo GoCityPass新加坡曼谷通票 2018-06-13 09:56

    什么意思?返回2进制,而不是字符串?

    点赞 1 评论 复制链接分享

相关推荐