我可以在另一个GET请求中包装GET请求吗?

时间:2014-02-04 12:26:43

标签: c# web-services rest

我可能正在接近这个错误,但我尝试做的是使用GET请求,以便我可以将响应操作为我想要的格式。我需要能够从浏览器启动它,所以我考虑为此目的构建另一个RESTful服务。

即。 Service ABC使用GET请求返回一个字符串。我想拿那个字符串,做一些操作并返回它。我仍然需要能够从浏览器启动,所以我计划的是创建一个RESTful服务XYZ,其中XYZ中的GET请求启动对ABC的GET请求的调用,获取该响应,转换它进入我的收藏,并返回该集合。然后我会在MVC中显示。

首先:这是一个愚蠢的选择吗?我不太了解不同类型的服务。

其次:我已经能够使用Console客户端获取ABC服务数据,但不能使用基于Web的客户端或服务。这是预期的吗?

代码:

string webPath = @"http://ABCService.co.uk/";
string methodCall = @"methodABC/uid";
RestClient restClient = new RestClient(webPath);
RestRequest request = new RestRequest(methodCall, Method.GET);
var restResponse = restClient.Execute(request);
var content = restResponse.Content;

这在控制台中工作正常(我实际上也只能使用WebClient获取数据),但这两种方法都不适用于MVC控制器或服务。我只是在restResponse中将它作为ErrorException:

  

例外:" {"无法连接到远程服务器"的InnerException   = {"连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接   失败,因为已连接的主机无法响应80.64.52.36:80"}

服务ABC启动并运行,可以从浏览器和控制台应用程序访问。请注意,我无法更改Service ABC中的任何设置。

非常感谢

1 个答案:

答案 0 :(得分:0)

我通过将代理设置为null来解决此问题,而不是使用默认代理。

public string ReadWebReport(string path)
    {
        string str = String.Empty;

        HttpWebRequest Request = WebRequest.Create(path) as HttpWebRequest;
        Request.Method = "GET"; //Or PUT, DELETE, POST
        Request.ContentType = "application/x-www-form-urlencoded";
        Request.Proxy = null; //<-- inserted line


        using (HttpWebResponse Response = Request.GetResponse() as HttpWebResponse)
        {
            if (Response.StatusCode != HttpStatusCode.OK)
                throw new Exception("The request did not complete successfully and returned status code " + Response.StatusCode);
            using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
            {
                str = Reader.ReadToEnd();
            }
        }

        return str;
    }
相关问题