.net跨域访问java写的接口,,有谁会?
WEB API, JSON数据交换。
具体怎么写啊
httpwebrequest
具体怎么写啊
@itwwg:
//这是get try { string uri = string.Format("{0}/CheckService", AppConfig.ServiceUrl); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri); wr.Credentials = CredentialCache.DefaultCredentials; wr.ContentLength = 0; wr.Method = "GET"; using (HttpWebResponse resp = (HttpWebResponse)wr.GetResponse()) { if (resp.ContentLength <= 0) { return false; } else { using (Stream respStream = resp.GetResponseStream()) { StreamReader sr = new StreamReader(respStream); string result = sr.ReadToEnd(); return result == "OK"; } } } } catch (Exception) { return false; }
@itwwg:
//这是post try { MessageInfo messageInfo = new MessageInfo(); messageInfo.Message = message; messageInfo.MessageTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string uri = string.Format("{0}/InsertDeviceMessageInfo", AppConfig.ServiceUrl); byte[] bs = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageInfo)); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri); wr.Credentials = CredentialCache.DefaultCredentials; wr.Method = "POST"; wr.Timeout = 5000; wr.ContentLength = bs.Length; wr.ContentType = "application/json"; using (Stream reqStream = wr.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } using (HttpWebResponse resp = (HttpWebResponse)wr.GetResponse()) { if (resp.ContentLength <= 0) { return -1; } else { using (Stream respStream = resp.GetResponseStream()) { StreamReader sr = new StreamReader(respStream); string result = sr.ReadToEnd(); return int.Parse(result); } } } } catch { return -1; }
@MrNice: 服务器端在我.net开发中不写代码,,代码只写js端
你说的跨域是 在.NET 服务端访问JAVA API 还是 在客户端浏览器跨域访问呢?
我的服务器端以jsonp协议去访问他们的数据
@itwwg:
jsonp 是jquery 浏览器脚本 怎么会是服务端呢
6楼说的很对 服务端没有跨域这个概念
请求方式都是一样的
只有在浏览器上,才有跨域这一说. .net代码不能在浏览器执行.所以你这问题不存在.
我也是这么觉得的
JSONP 可以跨域访问