문제

URL 인수 만있는 간단한 HTTP 게시물을 엔드 포인트로 만들려고합니다.

적어도 다음 지침을 이해하는 방법은 다음과 같습니다.

URL이라는 단일 매개 변수, 변경된 피드 주소로 해당 주소에 게시하십시오.

XML-RPC 방법과 마찬가지로 피드가 변경되었는지 확인하고 가입자에게 통지합니다.

이벤트가 기록됩니다. 리턴 값은 두 가지 속성의 성공과 MSG가있는 XML 메시지입니다.

이것은 현재 내 코드입니다.

        public static void ping(string feed)
    {
        HttpWebResponse response = MakeRequest(feed);
        XmlDocument document = new XmlDocument();

        document.Load(response.GetResponseStream();
        string success = document.GetElementById("success").InnerText;
        string msg = document.GetElementById("msg").InnerText;

        MessageBox.Show(msg, success);
    }
        private static HttpWebResponse MakeRequest( string postArgument)
    {
        string url = path + "?" + UrlEncode(postArgument);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        return (HttpWebResponse)request.GetResponse();
    }
    private static string UrlEncode( string value)
    {
        string result;
        result= HttpUtility.UrlEncode("url") + "=" + HttpUtility.UrlEncode(value);
        return result;
    }

서버에서 잘못된 응답을 받고있어서 어떻게 든 잘못하고 있다고 가정합니다. 다음은 응답입니다.

문서의 최상위 레벨에서 유효하지 않습니다. 오류 처리 리소스 파일 : /// c : /users/admin/appdata/local/temp/vsd1.tmp.xml ...

rue ^

어떤 아이디어 ??

미리 감사드립니다

도움이 되었습니까?

해결책 3

내가 찾은 코드는 다음과 같습니다.

스트림 본문에 매개 변수를 인코딩하는 HTTP 게시물을 사용합니다. 이것은 우리에게 콘텐츠 본문으로서 "URL-FooBar"를 제공합니다. 신체의 콘텐츠 유형, 배치, 경계 등이 없습니다.

        private static HttpWebResponse MakeRequest(string path, string postArgument)
    {
        //string url = path + "?" + UrlEncode(postArgument);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

        request.Method = "POST";
        string boundary = Guid.NewGuid().ToString().Replace("-", "");
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        Stream stream = request.GetRequestStream();
        string result = string.Format("url={0}", postArgument);
        byte[] value = Encoding.UTF8.GetBytes(result);
        stream.Write(value, 0, value.Length);
        stream.Close();

        return (HttpWebResponse)request.GetResponse();
    }
        public static void ping(string server, string feed)
    {
        HttpWebResponse response = MakeRequest(server, feed);
        XmlDocument document = new XmlDocument();
        string result = GetString(response.GetResponseStream());
        try
        {
            document.LoadXml(result);
        }
        catch
        {
            MessageBox.Show("There was an error with the response", "Error");
        }
        //MessageBox.Show(msg, success);

    }
    public static string GetString(Stream thestream)
    {
        int n = thestream.ReadByte();
        byte[] bytes = new byte[n];
        thestream.Read(bytes, 0, n);
        return Encoding.ASCII.GetString(bytes);
    }

GetString에 대한 호출은 디버깅 목적으로 만 사용되며 엄격하게 필요하지 않습니다.

저를 올바른 길로 데려가면서 여기에 갇힌 모든 분들께 감사드립니다.

다른 팁

.NET의 API를 알지 못하지만 : "쿼리 인수가있는 URL에서 게시물을 실행"하는 지침을 이해하고 포스트 본문에는 URL = foobar 매개 변수가 있어야합니다.

IOW : 사후 수치를 취하고 URL에 추가하는 대신 URL을 호출하고 메시지 본문에 올바르게 인코딩 된 URL = foobar를 제공해야합니다.

또한 : '허용'헤더 요청을 설정하는 것을 보지 못합니다. 서버가 응답 할 형식을 인식하는 데 사용하는 경우 중요 할 수 있습니다.

.NET에서 양식 게시물을 수행하는 가장 좋은 방법은 WebClient 클래스를 사용하는 것입니다. 해당 클래스는 게시물 (엔티티 본문의 매개 변수 포함) 또는 get (쿼리 문자열 인수로 인코딩 된 매개 변수 포함)로 데이터를 보내기 위해 올바른 작업을 수행합니다.

그리고 당신은 우리에게 무엇이 잘못되었는지 알아 내기 위해 고객이 돌려주는 실제 예외를 보여 주어야합니다 ..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top