A-A+

Http请求出错调试

2016年01月20日 C#基础知识 评论 1 条 阅读 563 次

Http请求出错调试

public static string RequestLongConnection()
{
string url = "接口地址";
bool iskeepConnect = true;
string requestData = "jsonParam={\"ServiceCode\": \"ServiceCode\"}";

byte[] requestByte = Encoding.UTF8.GetBytes(requestData);
string responseXml = string.Empty;
Encoding encoding = Encoding.GetEncoding("UTF-8");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
if (iskeepConnect)
{
request.KeepAlive = true;
request.Timeout = 1000 * 60 * 6;
}
else
{
request.KeepAlive = false;
}

request.ProtocolVersion = HttpVersion.Version10;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestByte.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(requestByte, 0, requestByte.Length);
}

try
{
// 接收返回的页面
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader rreadStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
responseXml = rreadStream.ReadToEnd();
}
}
}
catch (WebException ex)
{
HttpWebResponse res = (HttpWebResponse)ex.Response;
using (StreamReader respStream = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
{
responseXml = respStream.ReadToEnd();
}
}

return responseXml;
}

标签:

1 条留言  访客:1 条  博主:0 条

  1. avatar 香港独立IP主机

    感觉现在看到代码就头痛给,因为很多都看不懂了,越看越伤心了

评论已关闭!

Copyright © 极品飞鸽 保留所有权利.   Theme  Ality 蜀ICP备14015766号-1

用户登录

分享到: