HTTPS POST PHP에서 C#까지
-
10-07-2019 - |
문제
데이터를 조달하고 다시 전송하려면 제 3 자 HTTPS URL에 게시해야합니다. 그리고 내가 예를 들어있는 것은 다음과 같습니다.
$signature= foo_string;
$data_to_post = json_dictionary;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("JSON-Signature: $signature"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
ASP .NET C# 2.0과 함께 작업 할 때이 포트를 포트해야하지만 항상 자동 오류가 발생하지 않습니다.
여기에 내가하는 일이 있습니다.
HttpWebRequest q = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);
q.Method = "POST";
q.Headers.Add("JSON-Signature:" + GetSignature(data));
q.ContentType = "application/json";
q.UseDefaultCredentials = false;
q.Credentials = new NetworkCredential(user,pwd, Host);
byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
q.ContentLength = data.Length;
Stream oStream = q.GetRequestStream();
StreamWriter oWriter = new StreamWriter(oStream);
oWriter.Write(buffer);
oWriter.Close();
HttpWebResponse reps = q.GetResponse() as HttpWebResponse;
나는 이것에 대해 찾을 수있는 모든 질문을 모두 읽었지만 개선은 얻지 못합니다. 미리 감사드립니다!
해결책
글쎄, 당신이 잘못하고있는 한 가지는 길이가 바이트 길이와 동일합니다 캐릭터. 컨텐츠 길이에 Buffer.length를 사용해야합니다. 당신은 또한 전화합니다 StreamWriter.Write
a 바이트 배열. 당신은 그렇게해서는 안됩니다 - 이미 인코딩을 수행 했으므로 스트림을 사용해야합니다.
byte[] buffer = Encoding.UTF8.GetBytes(data);
q.ContentLength = buffer.Length;
using (Stream stream = q.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
이제 인증 문제는 해결되지 않습니다. 당신은 그 단지 설정을 찾을 수 있습니다 PreAuthenticate
그래도 해결합니다.
q.PreAuthenticate = true;
그래도 작동하지 않으면 실행하는 것이 좋습니다 Wireshark CURL을 통한 요청과 .NET의 요청의 차이점을 살펴보십시오.
다른 팁
인증에 호스트를 공급해서는 안된다고 생각합니다 ...
q.Credentials = new NetworkCredential(user,pwd);
다음과 같은 것입니다.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);
request.Method = "POST";
request.Headers.Add("JSON-Signature:" + GetSignature(data));
request.ContentType = "application/json";
request.UseDefaultCredentials = false;
request.Credentials = new NetworkCredential(user, pwd);
byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
request.ContentLength = buffer.Length;
using (Stream oStream = request.GetRequestStream()) {
oStream.Write(buffer, 0, buffer.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
// load data from response here
}
또한 각 요청에서 서비스 포인트 유효성 검사 대의원을 할당하지 않아야합니다. 이는 유효성 검사가 여러 번 수행되기 때문에 요청이 점점 느려질 수 있으며 메모리 누출이기도합니다.
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
ASP.NET에 Curlopt_userpwd를 추가하는 방법은 다음과 같습니다.
private async Task<string> Execute(string url, string query, string user, string pasword)
{
HttpClient httpClient = new HttpClient();
var baseUri = new Uri(url, UriKind.Absolute); // e.g. http://somedomain.com/endpoint
Uri request = new Uri(baseUri, query); // with query e.g. http://somedomain.com/endpoint?arg1=xyz&arg2=abc
// Add a new Request Message
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, request);
// add headers -> CURLOPT_USERPWD equivalent
var encodedStr = Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", user, password)));
var authorizationKey = "Basic" + " " + encodedStr; // Note: Basic case sensitive
requestMessage.Headers.Add("Authorization", authorizationKey);
// if POST - do this instead
// content
//HttpContent content = new StringContent(jsonContent); // string jsonContent i.e. JsonConvert.SerializeObject(YourObject);
//requestMessage.Content = content;
//requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// execute
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
var responseString = await responseMessage.Content.ReadAsStringAsync(); // reads it as string;
// if json and you need to convert to an object do this
// var myresponse = JsonConvert.DeserializeObject<YourMappedObject>(responseString);
return responseString;
}
제휴하지 않습니다 StackOverflow