简体   繁体   English

C#HttpClient Uri值

[英]C# HttpClient Uri value

I'm working on creating a WindowsPhone 8.1 app, and I'm having trouble getting the client to access the database. 我正在创建WindowsPhone 8.1应用程序,但无法让客户端访问数据库。 I created a server-side project that works fine using web api. 我创建了一个服务器端项目,可以使用Web api正常运行。 I'm able to run debugger and add "/api/entityName" to return a list of all the rows in that table or just a single row if I specify an id value. 我能够运行调试器并添加“ / api / entityName”以返回该表中所有行的列表,或者如果我指定了id值,则仅返回一行。 After doing a lot of testing on the client side, I think I've pinpointed my error: the uri value. 在客户端进行了大量测试之后,我认为我已经指出了我的错误:uri值。 Can someone tell me what value I should be passing into the uri constructor? 有人可以告诉我应该将什么值传递给uri构造函数吗? Here's the method that I'm using in the client: 这是我在客户端中使用的方法:

public async void GetBars()
    {
        var uri = new Uri("http://localhost:20672/tables/bars"); //seems like this value is the problem
        var httpClient = new HttpClient();

        barsListBox.Items.Add("right before try starts");
        // Always catch network exceptions for async methods
        try
        {
            barsListBox.Items.Add("try started");
            var result = await httpClient.GetStringAsync(uri); //never gets past this line. goes straight to finally
            barsListBox.Items.Add("right before linq");
            var queryAllBars =
                from bar in result
                select bar;
            barsListBox.Items.Add("linq finished");
            foreach (var bar in queryAllBars)
            {
                barsListBox.Items.Add(bar);
            }
        }
        catch
        {
            // Details in ex.Message and ex.HResult.       
        }
        finally
        {
            barsListBox.Items.Add("finally executed");
            httpClient.Dispose();
        }


    }

The database is a SQL Server database being hosted on Azure. 该数据库是托管在Azure上的SQL Server数据库。 I've tried using the URL listed for the database in Azure, my current IP address, just about every combination of using/excluding the port number, etc but nothing seems to work. 我尝试使用Azure中为数据库列出的URL,我当前的IP地址,几乎使用/排除端口号的每种组合等,但是似乎没有任何效果。 I don't get any errors, but I also don't get anything displayed in the UI (except for the testing statements I've entered). 我没有任何错误,但是我也没有在用户界面中显示任何内容(输入的测试语句除外)。 Try started gets printed, but right before linq doesn't, which is why I believe my problem is coming from the uri value being passed int GetStringAsync. 尝试开始打印,但是在linq之前不会打印,这就是为什么我认为我的问题来自传递给int GetStringAsync的uri值。 Thanks for the help. 谢谢您的帮助。

You can Try like this 你可以这样尝试

                using (var client = new HttpClient())
                {

                    client.BaseAddress = new Uri("http://Ipaddress/mammo/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = await client.GetAsync("api/controllername");

                        if (response.IsSuccessStatusCode)
                        {   
                           IList<something> data = await response.Content.ReadAsAsync<IList<something>>();
                        }
               }

Thanks to @Midhun's answer and other answers I received, I was able to get the bars to print correctly. 感谢@Midhun的回答和我收到的其他回答,我得以使这些条正确打印。 I used Midhun's code above with the uri of my hosted database (the azurewebsites.net url) instead of my localhost database. 我在托管数据库的uri(azurewebsites.net url)而非本地数据库中使用了Midhun的代码。 I also changed the IList to a String and wrote a method that picks out the values using IndexOf and Substring as follows: 我还将IList更改为String,并编写了一种使用IndexOf和Substring挑选值的方法,如下所示:

start = bar.IndexOf("BarID", start) + 7;
int end = bar.IndexOf(",", start);
int id = Convert.ToInt32(bar.Substring(start, (end - start)));
Bars newBar = new Bars(id, name, street, city, state, phone, zip);
Bars.barsList.Add(newBar);

I then created a static list in my Bars model to add the newly created Bars items to. 然后,我在Bars模型中创建了一个静态列表,以向其中添加新创建的Bars项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM