Отправить post запрос на JSON - C#
Формулировка задачи:
Всем привет , использую WebBrowser что бы отправлять пост запросы , и для сайта realty.mail.ru нужно отправить сюда --- http://pro.realty.mail.ru/add/jsonSave/ (так показывай сниффер) , но при попытке из программы передать данные на этот адрес , предлагает скачать файл JSON
вот код
webBrowser1.Navigate("http://pro.realty.mail.ru/addblock/"); String postData = "block_uid=&last_version=&created_at=&payer_id=5006502&bargain_type=sale&type_id=9&client_id=5006691&price_origin=1111111111¤cy=RUR&unit_type=total&description=http%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2Fhttp%3A%2F%2Fpro.realty.mail.ru%2Fadd%2FjsonSave%2F%D0%BC&classificators%5Brooms_count%5D%5Bvalue%5D=5&classificators%5Bcommon_area%5D%5Bvalue%5D=66&classificators%5Bkitchen_area%5D%5Bvalue%5D=22&classificators%5Bliving_area%5D%5Bvalue%5D=44&classificators%5Bfloor%5D%5Bvalue%5D=5&classificators%5Bfloors_count%5D%5Bvalue%5D=12&classificators%5Brenovation%5D%5Bvalue_id%5D=0&classificators%5Bflat_type%5D%5Bvalue_id%5D=0&classificators%5Bbathroom_type%5D%5Bvalue_id%5D=0&classificators%5Brooms_area%5D%5Bvalue%5D=&classificators%5Bhouse_type%5D%5Bvalue_id%5D=0&classificators%5Bbuilding_year%5D%5Bvalue%5D=&classificators%5Bname%5D%5Bvalue%5D=&classificators%5Bbuilding_serial%5D%5Bvalue%5D=&classificators%5Bceil_height%5D%5Bvalue%5D=&latitude=53.889947&longitude=27.573334&object_id%5B%5D=1111374&object_id%5B%5D=1111375&geo_relations%5Bsubway%5D%5Bobject_id%5D=0&geo_relations%5Bsubway%5D%5Bvalue%5D=&geo_relations%5Bsubway%5D%5Brelation_type_id%5D=4&raw_address=%D0%B7%D0%B0%D1%81%D0%BB%D0%B0%D0%B2%D1%81%D0%BA%D0%B0%D1%8F+25&company_name=Vlad+Shelepkov&contact_phone-country=1111374&contact_phone=%2B375+(29)+752-86-77&contact_email=crag.hack%40mail.ru&external_url=&is_agreed=1"; byte[] Bytes = Encoding.UTF8.GetBytes(postData); string AdditionalHeaders = "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine; webBrowser1.Navigate("http://pro.realty.mail.ru/add/jsonSave/", "", Bytes, AdditionalHeaders);
Решение задачи: «Отправить post запрос на JSON»
textual
Листинг программы
function GetRequest([string] $url, [System.Net.CookieContainer] $cookieContainer, [string] $postParams, [string] $userAgent) { [system.Net.HttpWebRequest] $request = [system.Net.HttpWebRequest]::Create($url) $request.CookieContainer = $cookieContainer $request.UserAgent = $userAgent if (![string]::IsNullOrEmpty($postParams)) { $request.Method = "POST" $request.ContentType = "application/x-www-form-urlencoded" $request.ContentLength = $postParams.Length [System.IO.TextWriter] $requestWriter = New-Object System.IO.StreamWriter($request.GetrequestStream()) $requestWriter.Write($postParams) $requestWriter.Close() $requestWriter.Dispose(); } return [system.Net.HttpWebRequest] $request } function GetContentFromUrl([System.String] $url, [System.Net.CookieContainer] $cookieContainer, [System.String] $postParams, [string] $userAgent) { try { $request = GetRequest $url $cookieContainer $postParams $userAgent $Response = $request.GetResponse() [System.IO.TextReader] $contentStream = New-Object System.IO.StreamReader($Response.GetResponseStream()) $content = [System.Web.HttpUtility]::HtmlDecode($contentStream.ReadToEnd()) $contentStream.Close() $Response.Close() if($Response.StatusCode -ne 200) { throw 'non-200 status code' } if ($content.Contains('Забыли пароль?')) { throw 'Неверные логин-пароль' } return $content } catch [System.Net.WebException] { try { # Write-Host $Error[0] -foregroundcolor "magenta" $exception = $_.Exception $status = GetStatusDescription( $exception.Status) $Error = GetExceptionDetails( $exception) $errorInfo = GetErrorInfo($exception) $errorDescription = GetErrorDescription($exception) $responseText = GetFailedResponseText -exception ($exception) if($exception.Response -ne $null) { $exception.Response.Close() } SetResult -result $false -info $errorInfo -properties (GetAdditionalProperties -pingUrl $url -responseUri $exception.Response.ResponseUri -httpResponseStatusCode ($exception.Response.StatusCode) -errorCode ($ErrorCode.WebError) -errorDescription $errorDescription -responseText ($responseText)) Exit } catch [System.Exception] { [System.String] $resultText = "При получении ответа произошла ошибка :{0} {1} {2} " -f ($status, $exception, $exception.GetType()) SetResult -result $false -info $errorInfo -properties (GetAdditionalProperties -pingUrl $url -resultCount 0 -errorDescription $errorDescription -httpResponseStatusCode 0) Exit } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д