带有边界错误错误请求的Http Post请求

时间:2013-03-05 17:51:58

标签: c# http-post

您好我正在尝试Http Post请求但收到错误请求的错误我认为我在请求中做错了,如果有人可以帮助我。

这是请求

     POST /uapi/faxes/@me/0003*210 HTTP/1.1
     HOST uapi.voipnow.com
     Content-Length:469
     Content-Type: multipart/form-data; boundary=------------325343636
     ------------325343636-------- 
     Content-Disposition:form-data; name="files"; filename="/path/to/file/fax.txt"
     Content-Type;application/octet-stream

     This is my fax
     ------------325343636
     Content-Disposition:form-data; name="request";
     {
       "recipients":["7778888"]
     }
     ------------3253436360

这是此请求的代码

        try
           {
             String Token = "mytoken";
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
            HttpWebRequest httpWReq2 = (HttpWebRequest)WebRequest.Create("https://domainname/uapi/faxes/@me/0014*100/?accessRequestToken=" + Token);

            ASCIIEncoding encoding2 = new ASCIIEncoding();
            string postData2 =
            "------------325343636" + Environment.NewLine +
            "Content-Disposition:form-data; name=\"files\";filename=\"/path/to/file/fax.txt\";" + Environment.NewLine +
            "Content-Type:application/octet-stream;" + Environment.NewLine +
            "This is my fax" + Environment.NewLine +
            "------------325343636" + Environment.NewLine +
            "Content-Disposition:form-data; name=\"request\";" + Environment.NewLine +
            "{" + Environment.NewLine +
            "\"recipients\":[\"111111\"];" + Environment.NewLine +
            "}" + Environment.NewLine +
            "------------325343636";

            byte[] data2 = encoding2.GetBytes(postData2);
            httpWReq2.Method = "POST";
            httpWReq2.ContentType = "multipart/form-data; boundary=------------325343636";
            httpWReq2.KeepAlive = true;
            httpWReq2.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17";
            httpWReq2.ContentLength =data2.Length;
            httpWReq2.Host = "hostname";
            string result2 = "";

            Stream dataStream2 = httpWReq2.GetRequestStream();
            dataStream2.Write(data2, 0, data2.Length);
            dataStream2.Close();
            WebResponse response2 = httpWReq2.GetResponse();
            HttpWebResponse responce3 = (HttpWebResponse)httpWReq2.GetResponse();
            dataStream2 = response2.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader2 = new StreamReader(dataStream2);
            // Read the content.
            string responseFromServer2 = reader2.ReadToEnd();
           }

            catch (Exception ex)
        {
            WebException ex2 = (WebException)ex;
            if (ex2.Status == WebExceptionStatus.ProtocolError)
            {
                WebResponse resp = ex2.Response;
                using (Stream respstream = resp.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(respstream);
                    String finalerror = reader.ReadToEnd();
                }
            }

        }

错误

  

收件人参数中提供的值缺失或无效。该   参数必须引用任何电话号码

2 个答案:

答案 0 :(得分:0)

哦,我发现了潜在的问题。看起来您正在发送JSON对象而不是表单编码值。

 ------------325343636
 Content-Disposition:form-data; name="request";
 recipients=7778888
 ------------3253436360

尝试这些方法。

答案 1 :(得分:0)

您的边界不匹配。

 POST /uapi/faxes/@me/0003*210 HTTP/1.1
 HOST uapi.voipnow.com
 Content-Length:469
 Content-Type: multipart/form-data; boundary=------------325343636
 --------------325343636
 Content-Disposition:form-data; name="files"; filename="/path/to/file/fax.txt"
 Content-Type;application/octet-stream

 This is my fax
 --------------325343636
 Content-Disposition:form-data; name="request";
 {
   "recipients":["7778888"]
 }
 --------------325343636--

您的边界应为-(两个连字符)+您的边界。最后一个边界应遵循相同的公式,并在末尾加上两个字符(两个连字符)。

在您的示例中,您的闭合边界不同,并且在边界的前面没有多余的两个连字符。

要澄清一点,如果您的边界是“边界”,则看起来像这样:

 POST /uapi/faxes/@me/0003*210 HTTP/1.1
 HOST uapi.voipnow.com
 Content-Length:469
 Content-Type: multipart/form-data; boundary=a boundary
 --a boundary
 Content-Disposition:form-data; name="files"; filename="/path/to/file/fax.txt"
 Content-Type;application/octet-stream

 This is my fax
 --a boundary
 Content-Disposition:form-data; name="request";
 {
   "recipients":["7778888"]
 }
 --a boundary--