当前位置:编程学习 > C#/ASP.NET >>

c# winform程序 如何完成XMLHttpRequest

我在本地,用html页面建立了个XMLHttpRequest能成功,但是在winform窗体应用程序里面始终不能获取到正确的返回值,求解。 --------------------编程问答-------------------- 求解啊!自己先顶一个 --------------------编程问答-------------------- winform调用什么东西?

可以使用HttpWebRequest


 HttpWebRequest request = (HttpWebRequest) WebRequest.Create(@"http://somewhere.com/client.php?locationID=1");
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       string content = new StreamReader(response.GetResponseStream()).ReadToEnd();


代码来自:
http://stackoverflow.com/questions/6749540/how-can-i-get-a-value-from-a-http-request-from-a-windows-forms-client --------------------编程问答-------------------- 这个我都知道啊,但是我这里不得行啊,我调用一个地址,返回的东西都不是我需要的
 byte[] byteArray = dataEncode.GetBytes(parameters);
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
            webReq.Method = "POST";
            webReq.ContentType = "application/x-www-form-urlencoded";
            webReq.Headers.Add("x-requested-with", "XMLHttpRequest");
            webReq.ProtocolVersion = new Version("1.0"); 
            if (timeout.HasValue)
            {
                webReq.Timeout = timeout.Value;
            } 
            webReq.ContentLength = byteArray.Length;
            Stream newStream = webReq.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);//写入参数
            newStream.Close();
            WebResponse response = webReq.GetResponse();
            
            return response;

我这样写的,他返回了一个地址。

httpRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
httpRequest.open('post', '_url', true);
httpRequest.onreadystatechange = _onreadystatechange;
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send(_parm);
}
catch(e){} 
}

这样写都能正常的返回我需要的。不清楚啊有什么区别么 --------------------编程问答--------------------
引用 3 楼 hyx8763 的回复:
这个我都知道啊,但是我这里不得行啊,我调用一个地址,返回的东西都不是我需要的
 byte[] byteArray = dataEncode.GetBytes(parameters);
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
            webReq.Method = "POST";
            webReq.ContentType = "application/x-www-form-urlencoded";
            webReq.Headers.Add("x-requested-with", "XMLHttpRequest");
            webReq.ProtocolVersion = new Version("1.0"); 
            if (timeout.HasValue)
            {
                webReq.Timeout = timeout.Value;
            } 
            webReq.ContentLength = byteArray.Length;
            Stream newStream = webReq.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);//写入参数
            newStream.Close();
            WebResponse response = webReq.GetResponse();
            
            return response;

我这样写的,他返回了一个地址。

httpRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
httpRequest.open('post', '_url', true);
httpRequest.onreadystatechange = _onreadystatechange;
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send(_parm);
}
catch(e){} 
}

这样写都能正常的返回我需要的。不清楚啊有什么区别么


返回一个地址是不是301 redirect了。 --------------------编程问答--------------------
引用 3 楼 hyx8763 的回复:
这样写都能正常的返回我需要的。不清楚啊有什么区别么

你请求的页面需不需要登陆?如果是请模拟登陆后在请求此页面。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,