Section=ResponseStatusLine协议冲突
向接口发送请求的时候报协议冲突。首先,用火狐和谷哥直接输入地址,是可以获得返回值的。
但是当用IE的时候,却提示我要下载文件。
我自己想做一个接口测试的小工具,代码十分简单:
WebRequest Myrequest = WebRequest.Create(sRequestUrl);
Stream responseStream = Myrequest.GetResponse().GetResponseStream();
第一句是成功了的,第二句就报Section=ResponseStatusLine。
在客户端的CONFIG文件里加过所谓的
<httpWebRequest useUnsafeHeaderParsing = "true" />
了,也有把WebRequest换成httpWebRequest了,没用。设置了UserAgent,也没用。
下面是用火狐截取的响应头:
Connection keep-alive
Content-Length 764
Content-Type application/json; charset=utf8
Date Thu, 10 Oct 2013 08:59:53 GMT
X-Powered-By Express
接口程序是用nodejs做的。求大神解答。 GetResponse ResponseStatusLine --------------------编程问答-------------------- 木有人理我啊。 --------------------编程问答-------------------- 大家帮帮忙啊。。。。。 --------------------编程问答-------------------- 提示下载文件,应该是被请求的页面的MIME类型不对吧 --------------------编程问答-------------------- Content-Type application/json
是这个类型、
我已经用HttpWebRequest 跟着设置了属性。
myrequest.ContentType = "application/json";
myrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0";
myrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
但是结果一样。 --------------------编程问答-------------------- 后来改成TcpClient的方式就可以了,但我还是想用WEBREQUEST,怎么办呢?
private string GetHTMLTCP(string URL)
{
string strHTML = "";//用来保存获得的HTML代码
TcpClient clientSocket = new TcpClient();
Uri URI = new Uri(URL);
clientSocket.Connect(URI.Host, URI.Port);
StringBuilder RequestHeaders = new StringBuilder();//用来保存HTML协议头部信息
RequestHeaders.AppendFormat("{0} {1} HTTP/1.1\r\n", "GET", URI.PathAndQuery);
RequestHeaders.AppendFormat("Connection:close\r\n");
RequestHeaders.AppendFormat("Host:{0}\r\n", URI.Host);
RequestHeaders.AppendFormat("Accept:*/*\r\n");
RequestHeaders.AppendFormat("Accept-Language:zh-cn\r\n");
RequestHeaders.AppendFormat("User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n\r\n");
Encoding encoding = Encoding.UTF8;
byte[] request = encoding.GetBytes(RequestHeaders.ToString());
clientSocket.Client.Send(request);
//获取要保存的网络流
Stream readStream = clientSocket.GetStream();
StreamReader sr = new StreamReader(readStream, Encoding.UTF8);
strHTML = sr.ReadToEnd();
readStream.Close();
clientSocket.Close();
if (strHTML.IndexOf('{')>1)
strHTML = strHTML.Remove(0, strHTML.IndexOf('{'));
return strHTML;
} --------------------编程问答-------------------- HTTP/1.1 user/getRanking unknown
X-Powered-By: Express
Content-Type: application/json; charset=utf8
Content-Length: 1761
ETag: "753840642"
Set-Cookie: connect.sess=s%3Aj%3A%7B%7D.v02hofZQGxTFwo2k5V171KG6FInGl%2FvJubN9Bz%2BINSk; Path=/; Expires=Tue, 15 Oct 2013 07:15:56 GMT; HttpOnly
Date: Tue, 15 Oct 2013 06:15:56 GMT
Connection: close
这个是接口返回给我的消息头。请问要怎么做可以呢?
补充:.NET技术 , ASP.NET