HttpWebRequest - 流减少要发送的字节 [英] HttpWebRequest - stream cuts bytes to send

查看:57
本文介绍了HttpWebRequest - 流减少要发送的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试将图片发送到php服务器,但是流看起来像是用例如51000字节到276字节的数据切割字节数组。字节数组名称是发送,下面我附上了一个代码:


 


 


  public  String res; 

public String sendData( byte [] data, int timeOutInMs)
{
var url = " ; HTTP://www.phogis.com/tools/synchronizer/index.php" ;
//创建Web请求对象
HttpWebRequest webrequest =(HttpWebRequest)WebRequest.Create(url);
webrequest.Method = " POST" ;
webrequest.ContentType = " application / x-www-form-urlencoded" ;

byte [] fileContent = Encoding.UTF8.GetBytes(" upload =" );
toSend = new byte [data.Length + fileContent.Length];
Array.Copy(fileContent,toSend,fileContent.Length);
Array.Copy(data,0,toSend,fileContent.Length,data.Length);

webrequest.BeginGetRequestStream( new AsyncCallback(fileops_uploadRequestCallback),webrequest);
Thread.Sleep(1500);
返回 res;
}

void fileops_uploadRequestCallback(IAsyncResult asyncResult)
{
HttpWebRequest webRequest =(HttpWebRequest)asyncResult。 AsyncState;
Stream postStream = webRequest.EndGetRequestStream(asyncResult);
postStream.Write(toSend,0,toSend.Length);
postStream.Flush();
postStream.Close();
webRequest.BeginGetResponse( new AsyncCallback(fileops_uploadResponseCallback),webRequest);
}

void fileops_uploadResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest webRequest =(HttpWebRequest)asyncResult。 AsyncState;
HttpWebResponse response =(HttpWebResponse)webRequest.EndGetResponse(asyncResult);
使用(StreamReader reader = new StreamReader(response.GetResponseStream()))
{
res = reader.ReadToEnd();
}

String [] tmpArr = Regex.Split(res,"<%!#data#!%>" );
if (tmpArr.Length == 2)
res = tmpArr [1];
else
res = "" ;
}

解决方案


I认为这是因为Async Call。你可以增加线程睡眠,看看可能是一旦睡眠时间结束你正在结束过程。


如果你从一个控制台应用程序调用一般这可能是原因。由于您使用的是异步调用,请使用End方法作为等待模式而不是Thread.Sleep,这将是准确的。


 


 


Hi,

I'm trying to send a picture to php server but the stream looks like it cuts bytes array with data from example 51000 bytes to 276 bytes. The byte array name is toSend, below I've attached a code:

 

 

public String res;

public String sendData(byte[] data, int timeOutInMs)
        {           
            var url = "http://www.phogis.com/tools/synchronizer/index.php";
            // Create the web request object
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
            webrequest.Method = "POST";
            webrequest.ContentType = "application/x-www-form-urlencoded";
           
            byte[] fileContent = Encoding.UTF8.GetBytes("upload=");
            toSend = new byte[data.Length + fileContent.Length];
            Array.Copy(fileContent, toSend, fileContent.Length);
            Array.Copy(data, 0, toSend, fileContent.Length, data.Length);

            webrequest.BeginGetRequestStream(new AsyncCallback(fileops_uploadRequestCallback), webrequest);
            Thread.Sleep(1500);
            return res;
        }

        void fileops_uploadRequestCallback(IAsyncResult asyncResult)
        {
            HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
            Stream postStream = webRequest.EndGetRequestStream(asyncResult);
            postStream.Write(toSend, 0, toSend.Length);
            postStream.Flush();
            postStream.Close();
            webRequest.BeginGetResponse(new AsyncCallback(fileops_uploadResponseCallback), webRequest);
        }

        void fileops_uploadResponseCallback(IAsyncResult asyncResult)
        {
            HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                res = reader.ReadToEnd();
            }
           
            String[] tmpArr = Regex.Split(res,"<%!#data#!%>");           
            if (tmpArr.Length == 2)
                res = tmpArr[1];
            else
                res = "";
        }

解决方案

Hi,

I think it is because of the Async Call. Can you increase the thread sleep and see may be once the sleep time is over you are ending process.

If you call from a Console App generally this may be the cause. Since you are using Async call use End method as wait mode instead of Thread.Sleep which will be accurate.

 

 


这篇关于HttpWebRequest - 流减少要发送的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆