C#调用web api要不要考虑跨域?

-一个大坑 2017-07-05 08:48:39

string url = "http:*******/QueryMileage";
string param = "param={'regNO':['粤TSW364'],'begin':'2017-06-10 10:03:15','end':'2017-06-10 12:03:15'}";;
string result = request(url, param);

public string request(string url, string param)
{
string strURL = url + '?' + param;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}

总是提示无法连接到服务器
...全文
569 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-一个大坑 2017-07-05
  • 打赏
  • 举报
回复
和跨域没关系访问外网需要权限,申请权限就好了
-一个大坑 2017-07-05
  • 打赏
  • 举报
回复
引用 2 楼 diaodiaop 的回复:
域是指不同域名之间相互访问. 也就是 如果你用js访问webapi的时候 会出现这种问题.. 但是用后台代码是不存在"跨域"的问题的. 至于你的错误...是你的POST请求方法有问题. 因为是POST最起码的 你不能把你的POST数据 扔URL上面吧... 那叫get...

public static string PostHttp(string url, string data, Dictionary<string, string> headers)
        {
            HttpWebRequest request = null;
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version11;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.Timeout = 5000;
            foreach (var item in headers)
            {
                request.Headers.Add(item.Key, item.Value);
            }
            byte[] postData = Encoding.ASCII.GetBytes(data);
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length);
            }
            string msg = "";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    msg = reader.ReadToEnd();
                }
            }
            return msg;
        }
data是传参?headers是什么?CheckValidationResult是方法?没有
-一个大坑 2017-07-05
  • 打赏
  • 举报
回复
引用 4 楼 diaodiaop 的回复:
我看了下LZ以前的帖子.. 我似乎发现 你连什么是get 什么是post好像还不是很清楚.....
我一直以为get是直接查;post是传参然后接收返回值
by_封爱 2017-07-05
  • 打赏
  • 举报
回复
我看了下LZ以前的帖子.. 我似乎发现 你连什么是get 什么是post好像还不是很清楚.....
by_封爱 2017-07-05
  • 打赏
  • 举报
回复
另外 webapi跟webservice没任何关系.... 你的"服务端" 到底是什么 你自己知道吗??
by_封爱 2017-07-05
  • 打赏
  • 举报
回复
域是指不同域名之间相互访问. 也就是 如果你用js访问webapi的时候 会出现这种问题.. 但是用后台代码是不存在"跨域"的问题的. 至于你的错误...是你的POST请求方法有问题. 因为是POST最起码的 你不能把你的POST数据 扔URL上面吧... 那叫get...

public static string PostHttp(string url, string data, Dictionary<string, string> headers)
        {
            HttpWebRequest request = null;
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version11;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.Timeout = 5000;
            foreach (var item in headers)
            {
                request.Headers.Add(item.Key, item.Value);
            }
            byte[] postData = Encoding.ASCII.GetBytes(data);
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length);
            }
            string msg = "";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    msg = reader.ReadToEnd();
                }
            }
            return msg;
        }
-一个大坑 2017-07-05
  • 打赏
  • 举报
回复
使用Post方式调用Web Service,需要在服务项目配置文件Web.config中添加使用Http协议的配置,在<system.web>标签中添加<webServices> <protocols> <add name= "HttpPost"/></protocols></webServices>配置 加了这个后现在报错变了 無效的 URI: 無法剖析 Authority/Host。

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧