Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Http Post请求带有边界错误错误错误请求_C#_Http Post - Fatal编程技术网

C# Http Post请求带有边界错误错误错误请求

C# Http Post请求带有边界错误错误错误请求,c#,http-post,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-

嗨,我试图做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();
                }
            }

        }
这是我的请求代码

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

        }
错误

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


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

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

试着按照这些思路做些事情。

你的边界不匹配

 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--

你在哪个服务器上发帖?这是特定于供应商的。@DanielA.White ye先生,您是对的,这是特定于供应商的。我想知道我的请求中的PostData有什么问题。@DiskJunky我认为内容长度应该与PostData长度相同,是不是我错了?你应该详细说明你发布到的服务器。不可能知道它的确切期望是什么。因此在我的代码中,更改应该是“{”+Environment.NewLine+”收件人:111111“+Environment.NewLine+”}”+Environment.NewLine+我尝试了这个,但仍然得到samerror@Hiren我需要一个来自api的示例请求