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

本文提供了Java和C#两种语言实现网络POST请求的具体示例代码。Java部分展示了如何通过URL创建连接并发送UTF-8编码的消息,接收响应。C#部分则通过创建HTTP请求,发送二进制数据,并接收返回的二进制数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值