.net URL 을 통 해 POST 데 이 터 를 전송 하여 구체 적 으로 실현

4409 단어 URL푸 시POST
새로운 회사 에 도착 해서 MVC 와 다른 것들 을 다시 접촉 하기 시작 했다.그래서 많은 것 을 다시 주 웠 습 니 다.
얼마 전에 제3자 회사 와 도 킹 하 는 방법 을 쓰 라 고 했 습 니 다.상대방 이 제공 한 URL 을 통 해 데이터 post 를 전송 합 니 다.
웹.config 에 url 을 넣 었 습 니 다.

<add key="urlStrings" value="urladdress"/>
.CS 파일 에 있어 요.

private string postString = System.Configuration.ConfigurationManager.AppSettings["urlStrings"].ToString();
저 는 데 이 터 를 xml 텍스트 로 전송 하기 때문에 데 이 터 를 포장 한 다음 에 HttpWebRequest 를 통 해 데 이 터 를 보 내 달라 고 요청 합 니 다.

string body = string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?>
<Body>
<ValidId>{0}</ValidId>
<OrderId>{1}</OrderId>
<Count>{2}</Count>
<ValidTime>{3}</ValidTime>
<Remark/>
</Body>", consumption.Id, consumption.Order.AgentOrderId, consumption.Count, consumption.CreateTime.DateTimeToDateString("yyyy-MM-dd HH:mm:ss"));

                string request = BuildRequest(body);

                HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(postString);
                hwr.Method = "POST";
                hwr.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("openstack"));
                hwr.ContentType = "application/json";
                //hwr.Accept = "application/xml";
                hwr.AllowAutoRedirect = true;

                byte[] dates = Encoding.UTF8.GetBytes(request);
                int count = dates.Length;
                //Stream newStream = hwr.GetRequestStream();
                MemoryStream newStream = new MemoryStream();
                try
                {
                    log.Add(" ");
                    newStream.Write(dates, 0, dates.Length);
                    hwr.ContentLength = newStream.Length;
                    Stream requestStream = hwr.GetRequestStream();
                    newStream.Position = 0L;
                    newStream.CopyTo(requestStream);
                    newStream.Close();
                    requestStream.Close();
이곳 에서 내 가 주목 할 만 한 것 은 처음에 나의 최초의 Memory Stream 은 Stream 을 사용 했다 는 것 이다.하지만 Stream 데이터 흐름 은 알 수 없 는 오류 입 니 다.Stream 데이터 흐름 length 찾기 동작 을 할 수 없습니다.

나중에 저도 인터넷 을 통 해 해결 방법 을 찾 았 습 니 다.Memory Stream 으로 Stream 을 잠시 대체 하고 마지막 으로 Stream 의 일부 검색 작업 을 Memory Stream 에 올 려 놓 고 진행 한 다음 에 Memory Stream 의 CopyTo()방법 으로 데 이 터 를 Stream 데이터 흐름 에 가 져 왔 습 니 다.
마지막 은 데이터 수신 입 니 다.이 건 간단 합 니 다.

HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse();
                    Stream stream = null;
                   stream= hwResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
                    string file = reader.ReadToEnd();
                    UTF8Encoding UTF = new UTF8Encoding();
                    Byte[] Bytes = UTF.GetBytes(file);
                    file = UTF.GetString(Bytes);
이곳 은 데이터 인 코딩 에 대한 변환 이 있 습 니 다.저 는 UTF-8 인 코딩 으로 바 뀌 었 습 니 다.
마지막 으로 저 는 데 이 터 를 받 는 처리 입 니 다.제 가 받 은 것 도 xml 텍스트 형식의 데이터 이기 때문에 처리 작업 도 하고 후속 작업 도 편리 합 니 다.

HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse();
                    Stream stream = null;
                   stream= hwResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
                    string file = reader.ReadToEnd();
                    UTF8Encoding UTF = new UTF8Encoding();
                    Byte[] Bytes = UTF.GetBytes(file);
                    file = UTF.GetString(Bytes);
string strBody = TCodeServiceCrypt.Decrypt3DESFromBase64(GetElementValue(doc.Element("Response").Element("Body")), UserFunc.SecretKey);
                        XDocument xBody = XDocument.Parse(strBody);
                        string userId = GetElementValue(xBody.Element("Body").Element("UseId"));
이것 이 바로 내 가 이번에 사용 한 응용 프로그램 들이다.
저 는 신출내기 입 니 다.잘 부탁드립니다.

좋은 웹페이지 즐겨찾기