java中DataOutputStream.writeUTF(String)在c#中的替代

1 篇文章 0 订阅
java


    public static String webPost(String msg,String myurl){
        String ret = "";
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            URL url = new URL(myurl);
            conn = (HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeUTF(msg);
            dos.flush();
           
            int code = conn.getResponseCode();
            System.out.println("code   " + code);
            if (code == HttpURLConnection.HTTP_OK){
                dis = new DataInputStream(conn.getInputStream());
                ret = dis.readUTF();
//                System.out.println(ret);
            }
//            conn.disconnect();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (dis != null)
            {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }              
            if (dos != null)
            {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }               
            if (conn != null)
            {
                conn.disconnect();
            }
        }
        return ret;
    }
---------------------------------------------------------
c#


        /// <summary>
        /// 向指定URL使用POST方法发送数据的例程,本函数不进行错误处理
        /// </summary>
        /// <param name="strURL">URL字符串</param>
        /// <param name="bytSend">要发送的二进制数据</param>
        /// <param name="SendProgress">发送数据时的进度处理</param>
        /// <param name="AcceptProgress">接受数据时的进度处理</param>
        /// <returns>接受到的二进制数据</returns>
        private static byte[] HttpPostData(
            string strURL ,
            byte[] bytSend )
        {
            // 发送数据
            System.Net.HttpWebRequest myReq=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
            myReq.Method = "POST" ;
            System.IO.Stream myStream = myReq.GetRequestStream();
            int iCount = 0 ;
            int utflen = bytSend.Length;
            byte a = (byte) ((utflen >> 8) & 0xFF);
            byte b = (byte) ((utflen >> 0) & 0xFF);
            byte[] bytSendUTF = new byte[utflen+2];
            bytSendUTF[0] = a;
            bytSendUTF[1] = b;
            Array.Copy(bytSend,0,bytSendUTF,2,utflen);

            while( iCount < bytSendUTF.Length )
            {
                if( iCount + 1024 > bytSendUTF.Length)
                {
                    myStream.Write(bytSendUTF, iCount , bytSendUTF.Length - iCount );
                    iCount = bytSendUTF.Length ;
                }
                else
                {
                    myStream.Write(bytSendUTF , iCount , 1024);
                    iCount += 1024;
                }
            }//while
            myStream.Close();

            // 接受数据
            System.Net.HttpWebResponse myRes = null;
            myRes = myReq.GetResponse() as System.Net.HttpWebResponse ;

            myStream = myRes.GetResponseStream();
            System.IO.MemoryStream myBuf = new System.IO.MemoryStream(1024);
            byte[] bytBuf = new byte[1024];
            int ContentLength = (int)myRes.ContentLength ;
            int AcceptLength = 0 ;
            while(true)
            {
                int iLen = myStream.Read(bytBuf,0,1024);
                if(iLen ==0)
                    break;
                myBuf.Write(bytBuf,0,iLen);
                AcceptLength += iLen ;
                if( AcceptLength > ContentLength )
                    ContentLength = AcceptLength ;
            }//while
            myStream.Close();
            myRes.Close();
            myReq.Abort();
            byte[] bytReturn = myBuf.ToArray();
            myBuf.Close();
            return bytReturn;
        }// public static byte[] HttpPostData()

    }


在前端读取后端 Java 返回的 `DataOutputStream.writeFloat()` 的数据,需要使用前后端约定的数据传输格式,常见的有 JSON 和二进制流。 如果后端返回的是 JSON 格式的数据,那么在 Vue 可以使用 `axios` 库或者 `fetch` API 来发送 HTTP 请求并获取返回的数据。假设后端返回的 JSON 数据格式如下: ```json { "floatValue": 3.1415 } ``` 那么在 Vue 组件,可以这样读取: ```javascript import axios from 'axios' export default { data() { return { floatValue: null } }, mounted() { axios.get('/api/floatValue').then(response => { this.floatValue = response.data.floatValue }) } } ``` 如果后端返回的是二进制流格式的数据,可以使用 `fetch` API 并设置 `responseType` 为 `blob`,然后使用 `FileReader` 来读取二进制流数据。假设后端返回的二进制流数据是一个 32 位浮点数,那么在 Vue 组件可以这样读取: ```javascript export default { data() { return { floatValue: null } }, mounted() { fetch('/api/floatValue', { method: 'GET', responseType: 'blob' }).then(response => { return response.blob() }).then(blob => { const reader = new FileReader() reader.addEventListener('loadend', () => { const dataView = new DataView(reader.result) this.floatValue = dataView.getFloat32(0, true) }) reader.readAsArrayBuffer(blob) }) } } ``` 需要注意的是,这种方式需要前后端约定好二进制流数据的字节顺序(即大端序还是小端序)。上面的代码,`getFloat32` 方法的第二个参数为 `true` 表示使用小端序(即字节顺序为低位字节在前,高位字节在后)。如果后端返回的是大端序的数据,需要将第二个参数设置为 `false`。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值