Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#Monodevelop中创建Uri时出错_C#_.net_Rest_Ubuntu_Monodevelop - Fatal编程技术网

在C#Monodevelop中创建Uri时出错

在C#Monodevelop中创建Uri时出错,c#,.net,rest,ubuntu,monodevelop,C#,.net,Rest,Ubuntu,Monodevelop,我正在尝试做一个与API REST通信的桌面应用程序,然后我决定在我的xubuntu中使用MonoDevelop。我尝试使用构造函数从字符串创建Uri,但在创建对象Uri时,它会出现在我的MonoDevelop调试器中: stationUri{System.Uri} System.Uri绝对路径System.NullReferenceException:对象 引用未设置为对象的实例绝对URI System.NullReferenceException:对象引用未设置为 对象权限系统的实例。Nul

我正在尝试做一个与API REST通信的桌面应用程序,然后我决定在我的xubuntu中使用MonoDevelop。我尝试使用构造函数从字符串创建Uri,但在创建对象Uri时,它会出现在我的MonoDevelop调试器中:

stationUri{System.Uri}
System.Uri绝对路径System.NullReferenceException:对象 引用未设置为对象的实例绝对URI System.NullReferenceException:对象引用未设置为 对象权限系统的实例。NullReferenceException: 对象引用未设置为对象DnsSafeHost的实例 System.NullReferenceException:对象引用未设置为 对象片段System.NullReferenceException的实例: 对象引用未设置为对象主机的实例
System.NullReferenceException:对象引用未设置为 对象HostNameType System.NullReferenceException的实例: 对象引用未设置为对象的实例

URLConParameteros
https://api.thingspeak.com/channels/***/fields/4.json?api_key=***&results=2
string 由于安全原因,我没有显示完整的URL

以及与此错误关联的相应代码:

public string GetResponse_GET(string url, Dictionary<string, string> parameters)
{
    try
    {
        //Concatenamos los parametros, OJO: antes del primero debe estar el caracter "?"
        string parametrosConcatenados = ConcatParams(parameters);
        string urlConParametros = url + "?" + parametrosConcatenados;
        string responseFromServer = null;
        Uri stationUri = new Uri(urlConParametros);
        if(!stationUri.IsWellFormedOriginalString())
        {
            System.Console.WriteLine("Url Vacía");
        }
        else
        {
            System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(stationUri);
            wr.Method = "GET";

            wr.ContentType = "application/x-www-form-urlencoded";

            System.IO.Stream newStream;
            // Obtiene la respuesta
            System.Net.WebResponse response = wr.GetResponse();
            // Stream con el contenido recibido del servidor
            newStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(newStream);
            // Leemos el contenido
            responseFromServer = reader.ReadToEnd();

            // Cerramos los streams
            reader.Close();
            newStream.Close();
            response.Close();
        }
        return responseFromServer;
    }
    catch (System.Web.HttpException ex)
    {
        if (ex.ErrorCode == 404)
            throw new Exception("Servicio Remoto No Encontrado: " + url);
        else throw ex;
    }
}

private string ConcatParams(Dictionary<string, string> parameters)
{
    bool FirstParam = true;
    string Parametros = null;

    if (parameters != null)
    {
        Parametros = "";
        foreach (KeyValuePair<string, string> param in parameters)
        {
            if(!FirstParam)
                Parametros+="&";
            Parametros+= param.Key + "=" + param.Value;
            FirstParam = false;
        }
    }

    return Parametros == null ? String.Empty : Parametros.ToString();
}
publicstringgetresponse\u-GET(字符串url、字典参数)
{
尝试
{
//职业卫生组织(OJO)的主要参数是:“对职业卫生的基本要求”?”
字符串参数concatenados=ConcatParams(参数);
字符串URLConParameteros=url+“?”+参数concatenados;
字符串responseFromServer=null;
Uri stationUri=新Uri(URLConParameteros);
如果(!stationUri.IsWellFormedOriginalString())
{
System.Console.WriteLine(“Url Vacía”);
}
其他的
{
System.Net.HttpWebRequest wr=(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(stationUri);
wr.Method=“GET”;
wr.ContentType=“应用程序/x-www-form-urlencoded”;
System.IO.Stream新闻流;
//拉瑞斯佩斯塔酒店
System.Net.WebResponse response=wr.GetResponse();
//塞维多雷西比多河
newStream=response.GetResponseStream();
System.IO.StreamReader=新的System.IO.StreamReader(新闻流);
//里莫斯·艾尔·孔泰多
responseFromServer=reader.ReadToEnd();
//塞拉莫斯洛斯河
reader.Close();
newStream.Close();
response.Close();
}
返回responseFromServer;
}
catch(System.Web.HttpException-ex)
{
如果(例如,错误代码==404)
抛出新异常(“Servicio Remoto No Encontrado:+url”);
否则就扔掉它;
}
}
私有字符串ConcatParams(字典参数)
{
bool FirstParam=true;
字符串参数=null;
if(参数!=null)
{
Parametros=“”;
foreach(参数中的KeyValuePair参数)
{
如果(!FirstParam)
参数+=“&”;
参数+=参数键+“=”+参数值;
FirstParam=false;
}
}
return Parametros==null?String.Empty:Parametros.ToString();
}
如果我完全运行代码,抛出下一个关联的stackTrace(我删除了敏感数据):

Gtk回调委托中出现异常 注意:应用程序可以使用GLib.ExceptionManager.UnhandledException来处理异常。 System.Reflection.TargetInvocationException:调用的目标已引发异常。-->System.NullReferenceException:对象引用未设置为对象的实例 在System.Net.WebRequest.Create(System.Uri requestUri)[0x00000]中:0 在/home//MonoDevelop Projects///MainWindow.cs:92中的MainWindow.GetResponse\u GET(System.String url,System.Collections.Generic.Dictionary`2参数)[0x0002b]处 在/home//MonoDevelop Projects///mainfown.cs:34中的mainfown.showAct(System.Object sender,System.EventArgs e)[0x0003f]处 at(包装器管理为本机)System.Reflection.monmethod:InternalInvoke(System.Reflection.monmethod,object,object[],System.Exception&) 在System.Reflection.MonMethod.Invoke(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性)[0x00038]中:0 ---内部异常堆栈跟踪的结束--- 在System.Reflection.MonMethod.Invoke(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性)[0x00053]中:0 在System.Reflection.MethodBase.Invoke(System.Object obj,System.Object[]参数)[0x00000]中:0 在0中的System.Delegate.DynamicInvokeImpl(System.Object[]args)[0x0010d]处 位于:0中的System.MulticastDelegate.DynamicInvokeImpl(System.Object[]args)[0x0000b]处 在0中的System.Delegate.DynamicInvoke(System.Object[]args)[0x00000]处 在GLib.Signal.ClosureInvokedCB(System.Object o,GLib.ClosureInvokedArgs args)[0x00067]中:0 在0中的GLib.SignalClosure.Invoke(GLib.ClosureInvokedArgs args)[0x0000c]处 在GLib.SignalClosure.marshallcallback(IntPtr raw_closure、IntPtr return_val、UInt32 n_参数值、IntPtr参数值、IntPtr调用提示、IntPtr封送数据)[0x00086]中:0 在GLib.ExceptionManager.RaiseUnhandledException(System.Exception e,布尔值为_-terminal)[0x00000]中:0 在GLib.SignalClosure.marshallcallback(IntPtr raw_closure、IntPtr return_val、UInt32 n_参数值、IntPtr参数值、IntPtr调用提示、IntPtr封送数据)[0x00000]in:0 在Gtk.Application.Gtk_main()[0x00000]中:0 在Gtk.Application.Run()[0x00000]中:0 在/home//MonoDevelop Projects///Program.cs:13中的.MainClass.Main(System.String[]args)[0x00012]

var uriBuilder = new UriBuilder(uri);
uriBuilder.Query = ConcatParams(parameters);
Uri stationUri = uriBuilder.Uri;
//if NOT well formed 
if(!stationUri.IsWellFormedOriginalString()) { //Note the `!` exclamation mark
    //...code removed for brevity
} else {
    //...code removed for brevity
}