HTTPWebResponse不返回任何内容

时间:2010-05-08 00:07:27

标签: c# asp.net http httpwebrequest httpwebresponse

我们公司与另一家名为iMatrix的公司合作,他们有一个用于创建我们自己的表格的API。他们已经确认我们的请求正在命中他们的服务器,但响应应该以参数确定的几种方式之一返回。我收到200 OK响应,但响应头中没有内容和内容长度为0.

这是网址: https://secure4.office2office.com/designcenter/api/imx_api_call.asp

我正在使用这个课程:

命名空间WebSumit {     public enum MethodType     {         POST = 0,         GET = 1     }

public class WebSumitter
{

    public WebSumitter()
    {
    }

    public string Submit(string URL, Dictionary<string, string> Parameters, MethodType Method)
    {
        StringBuilder _Content = new StringBuilder();
        string _ParametersString = "";

        // Prepare Parameters String
        foreach (KeyValuePair<string, string> _Parameter in Parameters)
        {
            _ParametersString = _ParametersString + (_ParametersString != "" ? "&" : "") + string.Format("{0}={1}", _Parameter.Key, _Parameter.Value);
        }

        // Initialize Web Request
        HttpWebRequest _Request = (HttpWebRequest)WebRequest.Create(URL);
        // Request Method
        _Request.Method = Method == MethodType.POST ? "POST" : (Method == MethodType.GET ? "GET" : "");

        _Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)";
        // Send Request
        using (StreamWriter _Writer = new StreamWriter(_Request.GetRequestStream(), Encoding.UTF8))
        {
            _Writer.Write(_ParametersString);
        }
        // Initialize Web Response

        HttpWebResponse _Response = (HttpWebResponse)_Request.GetResponse();


        // Get Response
        using (StreamReader _Reader = new StreamReader(_Response.GetResponseStream(), Encoding.UTF8))
        {
            _Content.Append(_Reader.ReadToEnd());
        }

        return _Content.ToString();
    }

}

}

我无法发布实际参数,因为它们属于实时系统,但您能查看此代码并查看是否有任何遗漏?

谢谢!

2 个答案:

答案 0 :(得分:2)

几个明显的问题:

  • 您不是对查询参数进行URL编码。如果您的值中有任何空格或特殊字符,服务器可能会对您的输入进行barf或截断它。
  • 即使方法是GET,你也试图在方法体中发送数据 - 这将失败。如果是GET,则需要在URL查询字符串上粘贴值。
  • 您尝试推出自己的WebClient版本,而不是仅使用WebClient。下面是一个WebClient示例,它处理参数的URL编码,正确处理GET和POST等。

public class WebSumitter 
{ 
    public string Submit(string URL, Dictionary<string, string> Parameters, MethodType Method) 
    { 
        // Prepare Parameters String 
        var values = new System.Collections.Specialized.NameValueCollection();
        foreach (KeyValuePair<string, string> _Parameter in Parameters) 
        { 
            values.Add (_Parameter.Key, _Parameter.Value);
        } 

        WebClient wc = new WebClient();
        wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)"; 
        if (Method == MethodType.GET) 
        {
            UriBuilder _builder = new UriBuilder(URL);
            if (values.Count > 0) 
                _builder.Query = ToQueryString (values);
            string _stringResults = wc.DownloadString(_builder.Uri);
            return _stringResults;
        }
        else if (Method == MethodType.POST)
        {
            byte[] _byteResults = wc.UploadValues (URL, "POST", values);
            string _stringResults = Encoding.UTF8.GetString (_byteResults);
            return _stringResults;
        }
        else
        {
            throw new NotSupportedException ("Unknown HTTP Method");
        }
    }
    private string ToQueryString(System.Collections.Specialized.NameValueCollection nvc)
    {
        return "?" + string.Join("&", Array.ConvertAll(nvc.AllKeys, 
            key => string.Format("{0}={1}", System.Web.HttpUtility.UrlEncode(key), System.Web.HttpUtility.UrlEncode(nvc[key]))));
    } 
}

答案 1 :(得分:1)

使用Fiddler查看是否有任何响应实际通过网络线路返回。听起来服务器正在向您发送一个空的200 OK响应。

相关问题