简体   繁体   English

C#WebRequest无法使用LinkShare中的此链接

[英]C# WebRequest won't work with this link from LinkShare

This is driving me a bit nuts. 这让我有点发疯。 I am trying to do something quite simple, and I have done it many times before. 我正在尝试做一些非常简单的事情,而我之前已经做了很多次。 Just trying to call a REST API. 仅尝试调用REST API。

I am trying to call GetMessage with endpoint = "http://feed.linksynergy.com/productsearch?token=717f8c8511725ea26fd5c3651f32ab187d8db9f4b208be781c292585400e682d&keyword=DVD", and it keeps returning empty string. 我正在尝试使用端点=“ http://feed.linksynergy.com/productsearch?token=717f8c8511725ea26fd5c3651f32ab187d8db9f4b208be781c292585400e682d&keyword=DVD”调用GetMessage,并且它会不断返回空字符串。 If I pass it any other valid URL, it will work. 如果我将其他任何有效的URL传递给它,它将起作用。 But if I just copy and paste the original URL into the web browser, it returns fine! 但是,如果我仅将原始URL复制并粘贴到Web浏览器中,它将返回正常值!

Can any smart developer tell me what's going on? 有什么聪明的开发商可以告诉我发生了什么吗?

Code below. 下面的代码。 Thanks in advance. 提前致谢。

James 詹姆士

public string GetMessage(string endPoint) { HttpWebRequest request = CreateWebRequest(endPoint); 公共字符串GetMessage(string endPoint){HttpWebRequest request = CreateWebRequest(endPoint);

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseValue = string.Empty;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                throw new ApplicationException(message);
            }

            // grab the response
            using (var responseStream = response.GetResponseStream())
            {
                using (var reader = new StreamReader(responseStream))
                {
                    responseValue = reader.ReadToEnd();
                }
            }

            return responseValue;
        }
    }

private HttpWebRequest CreateWebRequest(string endPoint) { var request = (HttpWebRequest)WebRequest.Create(endPoint); 私有HttpWebRequest CreateWebRequest(string endPoint){var request =(HttpWebRequest)WebRequest.Create(endPoint);

        request.Method = "GET";
        request.ContentLength = 0;
        request.ContentType = "text/xml";

        return request;
  }

Not sure why your setting ContentLength/ContentType - that is generally for HTTP POST , where there is a request body for which you write data to via a stream. 不知道为什么要设置ContentLength / ContentType-通常用于HTTP POST ,那里有一个请求主体 ,您可以通过流向其写入数据。

This is a HTTP GET , so there is no request body. 这是HTTP GET ,因此没有请求正文。 (just URI w/ query string) (只是带有查询字符串的URI)

This should work: 这应该工作:

using System;  
using System.IO;  
using System.Net;  
using System.Text;  

// Create the web request  
HttpWebRequest request = WebRequest.Create("http://www.someapi.com/") as HttpWebRequest;  

// Get response  
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
{  
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());  

    // Console application output  
    Console.WriteLine(reader.ReadToEnd());  
}

EDIT 编辑

@Gabe is also quite right - try this on another computer, that is isn't behind any kind of firewall/proxy server. @Gabe也很正确-在另一台计算机上尝试一下,它不在任何类型的防火墙/代理服务器后面。

My work PC was behind a proxy server, so in order to make REST-based HTTP calls, i needed to do this: 我的工作PC位于代理服务器之后,因此为了进行基于REST的HTTP调用,我需要这样做:

var proxyObject = new System.Net.WebProxy("http://myDomain:8080/", true);
System.Net.WebRequest req = System.Net.WebRequest.Create("http://www.someapi.com/");
req.Proxy = proxyObject;
proxyObject.Credentials = New System.Net.NetworkCredential("domain\username","password")

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

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