首页 新闻 赞助 找找看

求园子了解Mono和WebRequest.Create的进

1
悬赏园豆:140 [已关闭问题] 关闭于 2012-04-28 14:46
private void button1_Click(object sender, EventArgs e)
        {
              //Uri uri = new Uri("http://localhost:8080/web/");
Uri uri = new Uri("http://www.baidu.com"); WebRequest req = WebRequest.Create(uri); }


在Mono下调用以上代码时会引发以下异常:

Unhandled Exception: System.NotSupportedException: http://www.baidu.com/
at System.Net.WebRequest.GetCreator (string) <0x001af>
at System.Net.WebRequest.Create (System.Uri) <0x00039>
at XmlHelper.Form1.button1_Click (object,System.EventArgs) <0x0002a>
at System.Windows.Forms.Control.OnClick (System.EventArgs) <0x00050>
at System.Windows.Forms.Button.OnClick (System.EventArgs) <0x00037>

不知有谁遇到过,有解决办法

问题补充:

注意是在Mono下调用才报错,其余情况都是正常

枫江白露的主页 枫江白露 | 初学一级 | 园豆:69
提问于:2012-04-15 22:50
< >
分享
所有回答(4)
0
  private string HttpMethod(string data, string uri, string method, string contentType)
        {

            //Web访问对象
            string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri);
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
                //转成网络流
                byte[] buf = UnicodeEncoding.UTF8.GetBytes(data);
                //设置
                myRequest.Method = method;
                myRequest.ContentLength = buf.Length;
                //  myRequest.ContentType = "application/soap+JSON";
                myRequest.ContentType = contentType;
                myRequest.CookieContainer = cookie;
                // 发送请求
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(buf, 0, buf.Length);
                newStream.Close();
                // 获得接口返回值
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string ReturnXml = reader.ReadToEnd();
                reader.Close();
                myResponse.Close();
                return ReturnXml;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

用上面这段代码试试

az235 | 园豆:8483 (大侠五级) | 2012-04-16 08:35

您好,

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);

这句在Mono下运行还是会报我提到的错误。

支持(0) 反对(0) 枫江白露 | 园豆:69 (初学一级) | 2012-04-16 12:48
0

System.NotSupportedException一般是因为你的协议不是标准协议,比如你创建一个请求到 abc://aaa.bbb.ccc 这个地址,那么由于abc是未知的协议,会抛出NotSupportedException. 你贴在问题里的代码是你简化过的还是怎么来的,你debug一下,看看Uri的值到底是什么,如果还不行就用string代替Uri:

WebRequest req = WebRequest.Create("http://www.baidu.com");

看看是不是还会报错。

 

水牛刀刀 | 园豆:6350 (大侠五级) | 2012-04-16 09:57

您好,直接用string替换也是同样错误,注意只是在Mono下调用报错

支持(0) 反对(0) 枫江白露 | 园豆:69 (初学一级) | 2012-04-16 12:57
0

Mono好像还不是很成熟哦...有很多东西还不能正常使用的

你试一下使用WebClient看行不行啦

magic_evan | 园豆:206 (菜鸟二级) | 2012-04-16 13:17
0

怎么解决的?我也遇到这问题了。

我的换进是CentOS7.0 系统 下面 mono+jexus 

跟这个问题一样吗?

https://q.cnblogs.com/q/37805/

熊仔其人 | 园豆:212 (菜鸟二级) | 2016-12-15 10:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册