.net URL 을 통 해 POST 데 이 터 를 전송 하여 구체 적 으로 실현
얼마 전에 제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"));
이것 이 바로 내 가 이번에 사용 한 응용 프로그램 들이다.저 는 신출내기 입 니 다.잘 부탁드립니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Qiita 링크에 나타나지 않는 현상 확인가설·검증(0) 일람 라는 기사에, 가설·검증(51) 공개 산보는 기민하다(open source is agile)GitHub and docker 라는 줄이 있다. 이 행의 URL을 클릭하고 위 기사의 끝을 보면 가설...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.