开发者

HTTPWebResponse returning no content

开发者 https://www.devze.com 2022-12-30 02:18 出处:网络
Our company works with another company called iMatrix and they have an API for creating our own forms. They have confirmed that our request is hitting their servers but a response is supposed to come

Our company works with another company called iMatrix and they have an API for creating our own forms. They have confirmed that our request is hitting their servers but a response is supposed to come back in 1 of a few ways determined by a parameter. I'm getting a 200 OK response back but no content and a content-length of 0 in the response header.

here is the url: https://secure4.office2office.com/designcenter/api/imx_api_call.asp

I'm using this class:

namespace WebSumit { public enum Metho开发者_如何学CdType { 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();
    }

}

}

I cannot post the actual parameters because they are to the live system, but can you look at this code and see if there is anything that is missing?

Thanks!


Several obvious problems:

  • you're not URL-encoding your query parameters. If there are any spaces or special characters in your values, the server may barf on your input or truncate it.
  • you're trying to send data in the method body even if the method is GET -- this will fail. You need to stick values on the URL query string if it's a GET.
  • You're trying to roll your own version of WebClient instead of just using WebClient. Below is a WebClient sample which handles URL-encoding of parameters, handles GET and POST properly, etc.

.

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]))));
    } 
}


Use Fiddler to see whether any response is actually coming back across the network wire. It sounds like the server is sending you an empty 200 OK response.

0

精彩评论

暂无评论...
验证码 换一张
取 消